Home > Software design >  Given a classmethod reference, how to get a reference to the class in which it is defined?
Given a classmethod reference, how to get a reference to the class in which it is defined?

Time:08-18

I have a bound classmethod refference at some point in the code. I want to get the class itself from it. How can I do it?

class A:
    @classmethod
    def class_method(cls):
        ...
...

method_reference = A.class_method

...

reference_to_A = method_reference.bound_class # how to get it?

CodePudding user response:

Use __self__:

>>> class A:
...     @classmethod
...     def class_method(cls):
...             pass
...
>>> method_reference = A.class_method
>>> method_reference.__self__
<class '__main__.A'>
  • Related