为什么此代码会一直返回“无属性”错误?

时间:2013-12-22 23:54:14

标签: python

我的代码如下,其中sdl2ext.Entity是第三方类。

class Grid(sdl2ext.Entity):
    def __init__(self, world):
        self.w = 3
        self.h = 3
        super(Grid,self).__init__()

    def dump(self):
        print(self.w)

def run():
    world = sdl2ext.World()
    g = Grid(world)
    g.dump()

if __name__ == "__main__":
    run()

我得到的具体错误是行print(self.w)

AttributeError: object ''Grid'' has no attribute ''w''

这是否与初始化基础对象sdl2ext.Entity

无关

1 个答案:

答案 0 :(得分:2)

您应该阅读the code of the paren class。该类会覆盖许多特殊方法,包括可能与您的问题有关的__getattr__