class Robot:
"""this is a Robot class is used to declare a Robot"""
def __init__(self,name="tallapalli",build_yr=1990):
"""this method is use to intialize the data to the class variable"""
self.name=name
self.build_yr=build_yr
def hello(self):
"""this is a mmethod to use to send the hello world to the robot"""
print("hello i am "+self.name)
def set_name(self,name):
"""this mmethod is used to set the name"""
self.name=name
def get_name(self):
return self.name
def __repr__(self):
return "robot\""+ self.name+"\'"+str(self.build_yr)
def __str__(self):
return "robot\""+ self.name+"\'"+str(self.build_yr)
if __name__=="__main__":
z=Robot("nageswara rao",1991)
#print(z.get_name())
#z.set_name("chandu")
#z.hello()
Robot.str(z)
Robot.repr(z)
答案 0 :(得分:1)
__repr__
和__str__
是特殊方法,通常由函数调用方式由repr
和str
触发。
z=Robot("nageswara rao",1991)
str(z)
repr(z)
或者,如果要从类中调用方法,则必须使用其全名:
Robot.__str__(z)
Robot.__repr__(z)