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?

6
  • Why are you initializing the superclass after setting the private attributes? Commented Dec 22, 2013 at 23:59
  • Because of the answer given here: stackoverflow.com/questions/10670020/… . Specifically the definition for the CastSpell class given in the accepted answer. Commented Dec 23, 2013 at 0:01
  • You misunderstand. Why are you initializing the superclass after setting the private attributes, instead of before? Commented Dec 23, 2013 at 0:05
  • @BartoszKP could you elaborate on that at all? Commented Dec 23, 2013 at 0:05
  • @Ignacio, in the accepted answer for that linked question, the superclass is initialised after the private attributes. I thought it was ok to do that. Commented Dec 23, 2013 at 0:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

__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.
See the __getattr__ documentation: 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)
So @Martijn are you saying that getattr is not the problem?
@patchwork: I am saying __getattr__ cannot be the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.