AttributeError:类型对象'Robot'没有属性'repr'类型对象'Robot'没有属性'str'

时间:2016-06-29 05:50:40

标签: python python-3.x

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)

1 个答案:

答案 0 :(得分:1)

__repr____str__是特殊方法,通常由函数调用方式由reprstr触发。

z=Robot("nageswara rao",1991)

str(z)
repr(z)

或者,如果要从类中调用方法,则必须使用其全名:

Robot.__str__(z)
Robot.__repr__(z)