myol
myol

Reputation: 9848

Method reference in attribute before method is defined causing error

I don't understand why this works;

class parentClass(object):
    pass

class childClass(parentClass):

    def my_meth(var):
        print(var)

    a = {'meth': my_meth}

x = childClass.a['meth']("Helloworld")

Whilst this fails;

class parentClass(object):
    pass

class childClass(parentClass):

    a = {'meth': my_meth}

    def my_meth(var):
        print(var)

x = childClass.a['meth']("Helloworld")

It would seem that a class is read line by line on execution and if the method definition has not been parsed before it is referenced within an attribute, an error occurs?

Is this true? Why does this occur in class/static attributes whilst you can def methods in any order and reference them from other methods written above or below them?

Is there a way to keep the class attributes at the top of the class to preserve readability and consistent layout?

Upvotes: 2

Views: 44

Answers (1)

gilch
gilch

Reputation: 11681

It would seem that a class is read line by line on execution and if the method definition has not been parsed before it is referenced within an attribute, an error occurs?

Yes, that is true. Class definitions are executed from top to bottom, just like module-level code. You can even put things like if/else statements and for loops directly inside the class body.

Is there a way to keep the class attributes at the top of the class to preserve readability and consistent layout?

Your first example is fine. The order is not considered strange in Python.

That said, you do have potential alternatives. You could, for example, create a @classmethod to initialize class attributes at the top of the class, and then call immediately after the class declaration, e.g.

class childClass(parentClass):

    @classmethod
    def _initialize(cls)
        cls.a = {'meth': cls.my_meth}

    def my_meth(var):
        print(var)

childClass._initialize()

You could even write a class decorator to do this step for you (if you think that's prettier), as they execute after the class declaration code has completed execution.

Why does this occur in class/static attributes whilst you can def methods in any order and reference them from other methods written above or below them?

Executing a function definition is different from calling the function object it creates. The former just creates the function object and assigns its name to the local context. The latter runs its code.

Classes are just fancy dicts. You can swap attributes in and out of them at runtime. When you do a self.foo() inside of a method bar, the . operator is doing an attribute lookup of the foo attribute on the self object. It's a similar idea when using cls.foo() in a classmethod.

It's entirely possible to write a function that references an attribute that doesn't exist. It will fail with an AttributeError, of course, if you call it, but if somehow the attribute gets set later, then you can call it successfully.

Upvotes: 2

Related Questions