Does python inherit special methods?

** Execution environment **

I'm checking the output on the interpreter, so I'm omitting print ().

Name Version
python 3.4.3
ipython 3.1.0

Check what to inherit

When I checked with the special method __init __ (), I confirmed that the Super class __init __ () was executed in the undefined Sub class. As a precaution, we are checking inheritance other than special methods.

class Super():
    super_class_var = 'super_class_value'

    def __init__(self):
        self.super_instance_var = 'super_instance_value'

    @classmethod
    def __super_class_method__(cls):
        print('__super_class_method__')

    def __super_instance_method__(self):
        print('__super_instance_method__')

class Sub(Super):
    pass

\#Check class fields
Sub.super_class_var
\# => 'super_class_value'

Sub.__super_class_method__
\# => <bound method type.__super_class_method__ of <class 'special_method.Sub'> > 
Sub.__super_class_method__()
\# => __super_class_method__

\#Check instance fields
sub = Sub()
sub.super_instance_var
\# => 'super_instance_value'

sub.__super_instance_method__
\# => <bound method Sub.__super_instance_method__ of <special_method.Sub object at 0x10dcf0da0> > 
sub.__super_instance_method__()
\# => __super_instance_method__

If the attributes of the parent class are changed dynamically, will they be reflected in the child class?

The answer is yes!

\#Confirm in class
Super.new_var = 'new_var'
Sub.new_var
\# =>  'new_var'
'new_var' in dir(Sub)
\# => True

\#Check with instance
sub = Sub()
sub.new_var
\# => 'new_var'
'new_var' in dir(sub)
\# => True

Even if you change the attributes of the parent class later on the interpreter, it will be recognized by the child class.

The inherited attribute name is an alias

Does the child class have inherited methods separate from those of the parent class? Let's check it. The source code is a continuation from the last time.

id(Super.__super_class_method__)
\# => 4381481672
id(Sub.__super_class_method__)
\# => 4381481672
id(sub.__super_class_method__)
\# => 4381481672

id(Super.__super_class_method__) ==\
id(Sub.__super_class_method__)   ==\
id(sub.__super_class_method__)
\# =>  True

The method id (identifier) pointed to by all attribute names was the same. You can see that there is only one object for the class method __super_class_method__. The point to note here is that the attribute name __super_class_method__ is not the same, but this attribute name, that is, the id of the object pointed to by the label is the same. Let's check this in the source code.

foo = '1'
id(foo)
\# => 4361922800

bar = foo
id(bar)
\# => 4361922800

id(foo) == id(bar)
\# => True

What about the name resolution of inherited attribute names?

In this experiment, we confirmed that the special method of the parent class is also inherited. I also inherited the __method_name__ that I defined. Since this and the special method name are just labels, they are the same in terms of attribute names. However, I do not call it a special method that I have defined. A special method is a method that is indirectly called without directly writing method (). When you execute operators such as +,-, <=, the corresponding special methods are executed. Therefore, it is possible to change the behavior of the operator by rewriting the special method. For details, please check the official document below.

[3. Data Model — Python 3.4.3 Document](http://docs.python.jp/3/reference/datamodel.html?highlight=%E7%89%B9%E6%AE%8A%E3%83% A1% E3% 82% BD% E3% 83% 83% E3% 83% 89 #specialnames)

This experiment solved the question ** "Does python inherit special methods?" **. However, new questions have arisen.

If you hold it,

If you don't hold it

If anyone can understand it, please let me know.

Recommended Posts

Does python inherit special methods?
Convenient Python methods, etc.
[python] class basic methods
String object methods in Python
Python version does not switch
Dynamically call methods in Python
A special Python codec that seems to know but does not know
Dynamically define functions (methods) in Python
[Python] Inherit a class with class variables
Python classes and instances, instance methods