It's a basic story because my acquaintance was stumbling.
For example, suppose you define the following classmethod in Python:
python
class Parent(object):
name = "Parent"
@classmethod
def get_name(cls):
return cls.name
class Child(Parent):
name = "Child"
By the way, what is output when calling from Child at this time?
python
In : Child.get_name()
OUT: "Child"
Basically, the first argument defined in class binds the called instance or the class itself. Therefore, even if it is inherited, the class at the inherited destination will be entered. So, for example, it's OK if you don't make such a strange definition.
python
class Child(Parent):
name = "Child"
get_name = Parent.get_name
In this regard, the method basically has the context of "where it belongs", so in this case the method is executed according to the context of the referrer Parent, no matter what the class is.
Recommended Posts