1

My code is as follows, where sdl2ext.Entity is a third party class.

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()

The specific error that I get is with the line print(self.w):

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

Is this something to do with not initialising the underlying base object, sdl2ext.Entity?

patchwork
  • 1,161
  • 3
  • 8
  • 23

1 Answers1

2

You should read the code of the paren class. The class overwrites many special methods, including __getattr__ which likely has something to do with your problem.

stefanw
  • 10,456
  • 3
  • 36
  • 34
  • `__getattr__` is only called as a *fallback*; `self.w` is first looked up in the instance `__dict__`, `__getattr__` is only consulted if it is not found there. – Martijn Pieters Dec 23 '13 at 00:17
  • 1
    See the [`__getattr__` documentation](http://docs.python.org/2/reference/datamodel.html#object.__getattr__): *Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for `self`)* – Martijn Pieters Dec 23 '13 at 00:26
  • So @Martijn are you saying that __getattr__ is not the problem? – patchwork Dec 23 '13 at 08:49
  • @patchwork: I am saying `__getattr__` **cannot** be the problem. – Martijn Pieters Dec 23 '13 at 08:50