1

I'm trying to access a class method from the class variable as follows:

class A():
    a = A.b()

    @classmethod
    def b():
        return 5


print A.a

but i get the error:

NameError: name 'A' is not defined

What am I doing wrong?

1 Answer 1

1

You're using A within A. You should, first of all, put all of your initialization in an __init__ definition. And then use self to call upon itself.

class A():
    def __init__ (self):
        self.a = self.b()

    @classmethod
    def b(cls):
        return 5

print A().a
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I'm familiar with the other approach, but I want to use the class variable for this case. How can I achieve that behavior while using class variable. Should I define the method outside of the class?

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.