Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a classmethod from inside a class in Python

I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact that there will be the requirement to reinitialize the static members later on I would put this code into a classmethod.

My question: How can I call this classmethod from inside the class?

class Test():     # static member     x = None     # HERE I WOULD LOVE TO CALL SOMEHOW static_init!      # initialize static member in classmethod, so that it can be      #reinitialized later on again         @classmethod     def static_init(cls):         cls.x = 10 

Any help is appreciated!

Thanks in advance, Volker

like image 886
Volker Avatar asked Dec 16 '12 10:12

Volker


People also ask

How do you access a function inside a class in Python?

Accessing the method of a class To access the method of a class, we need to instantiate a class into an object. Then we can access the method as an instance method of the class as shown in the program below. Here through the self parameter, instance methods can access attributes and other methods on the same object.

How do you access a method from another class in Python?

Call method from another class in a different class in Python. we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A(self): {} class B: method_B(self): A.

Can a class method access instance variables Python?

There are two ways to access the instance variable of class: Within the class in instance method by using the object reference ( self ) Using getattr() method.

How do you call a method in class on itself?

In general, make sure self is the first argument in all class methods, and you can call that class method at any time using either self. method_name() if you are using it from within another class method or instance. method_name() if you're using it externally (where instance = Something() ). Wiggy A.


1 Answers

At the time that x=10 is executed in your example, not only does the class not exist, but the classmethod doesn't exist either.

Execution in Python goes top to bottom. If x=10 is above the classmethod, there is no way you can access the classmethod at that point, because it hasn't been defined yet.

Even if you could run the classmethod, it wouldn't matter, because the class doesn't exist yet, so the classmethod couldn't refer to it. The class is not created until after the entire class block runs, so while you're inside the class block, there's no class.

If you want to factor out some class initialization so you can re-run it later in the way you describe, use a class decorator. The class decorator runs after the class is created, so it can call the classmethod just fine.

>>> def deco(cls): ...     cls.initStuff() ...     return cls >>> @deco ... class Foo(object): ...     x = 10 ...      ...     @classmethod ...     def initStuff(cls): ...         cls.x = 88 >>> Foo.x 88 >>> Foo.x = 10 >>> Foo.x 10 >>> Foo.initStuff() # reinitialize >>> Foo.x 88 
like image 108
BrenBarn Avatar answered Sep 17 '22 13:09

BrenBarn