关于AttributeError: type object ‘XXX‘ has no attribute ‘XXX‘的问题

关于AttributeError: type object ‘XXXXXX’ has no attribute 'name’的报错的原因,先运行运行一段代码
例如:

class Person(object):

    def __init__(self,name):
        self.name=name

    def play(self):
        print('工作')

if __name__ == '__main__':
    p=Person('KB')
    p.play()
    print(Person.name)

运行结果为:

print(Person.name)
AttributeError: type object 'Person' has no attribute 'name'

错误原因:因为类调用了实例属性,
类是不能调用实例属性的

猜你喜欢

转载自blog.csdn.net/YZL40514131/article/details/120838681