python (2) requires self because the method is an instance method

It seems that it is popular to talk about self of python2 / 3. I also try to infuse the freshness.

class class1(object):
    def method1(self):
        pass

You can run this script as it is, but I recommend trying various things with repl. This script works with both python2 / 3, but the output of python2 (because it's a bit more primitive and intuitive, I've included it as well.

Class object

Beginners call it confusing, but in python the only thing you can call a "class object" is class1 here. This class1 can be confirmed by repl. type (class1) == "type".

print(class1)  # <class '__main__.class1'>
print(type(class1))  # <type 'type'>

This is clearly distinguished in docs.python.

9 . Classes — Python 3 \ .8 \ .3 Documentation

In other words, what I saw in the "class definition" was, strictly speaking, the creation of a type type instance.

method of

If you try some repls for class1 here, you can see that method1 can be called even in this state. These are of type instancemethod and are unbound. That is, it is not tied to an instance.

print(callable(class1))  # True
print(callable(class1.method1))  # True

print(class1.method1)  # <unbound method class1.method1>
print(type(class1.method1))  # <type 'instancemethod'>

So, in this state, you need to specify self to specify the instance.

Instance object

Create an instance.

instance1 = class1()
print(instance1)  # <__main__.class1 object at 0x7f19f6368910>

instance1 is called an instance object.

9 . Classes — Python 3 \ .8 \ .3 Documentation

method of

What happens to method1 in instance1?

print(instance1.method1) # <bound method class1.method1 of <__main__.class1 object at 0x7f19f6368910>>
print(callable(instance1.method1))  # True
print(type(instance1.method1)) # <type 'instancemethod'>
print(instance1.method1 == class1.method1) # True

It's still an instance method, but when I look at the repl, it's bound. Since it is called via the instance object, it is clear that == bound is associated with instance1.

Under this condition, python2 / 3 supplements the first argument without permission. This becomes self. That's why.

By the way, it's just a convention to name this "self", and "this" works fine.

Can be substituted

Since python functions are just instancemethods, they can be assigned outside the class / instance.

func1 = instance1.method1
print(func1) # <bound method class1.method1 of <__main__.class1 object at 0x7f4b663e6910>>

Since it is clear that instance1 is bound in the expression on the right side, func1 can be called with no arguments.

And self

As mentioned above

Both are of the instancemethod type internally, and the behavior on the function side has not changed. The caller is adjusting the book.

Since it is rare to call class1.method1 directly, it is necessary to add self in anticipation of execution from the instance object.

Static methods and classmethod

Of course, there are also static methods in other languages. You can remove the method arguments, but there is a safer way. It's a classemethod decorator.


class class2(object):
    def method1(cls):
        print(cls)

    @classmethod
    def method2(cls):
        print(cls)


print(class2.method1)  # <unbound method class2.method1>
print(class2.method2)  # <bound method type.method2 of <class '__main__.class2'>>

At the stage of class2, method2 is already bound of class2. This is still available when called from an instance.

instance2 = class2()
print(instance2.method1) # <bound method class2.method1 of <__main__.class2 object at 0x7f33073ed950>>
print(instance2.method2) # <bound method type.method2 of <class '__main__.class2'>>

But self

How about based on the above? Doesn't self, which I thought was troublesome, seem to be very important?

Recommended Posts

python (2) requires self because the method is an instance method
Python is instance
Why is the first argument of [Python] Class self?
Python is an adult language
What is an instance variable?
I got an AttributeError when mocking the open method in python
[Python] What is @? (About the decorator)
[python] What is the sorted key?
What is the python underscore (_) for?
Python> What is an extended slice?
Python in is also an operator
Hit a method of a class instance with the Python Bottle Web API
Find out the name of the method that called it from the method that is python
[Introduction to Python] What is the method of repeating with the continue statement?
The story of making Python an exe
Where is the python instantiation process written?
Installing Anaconda Python on an ECS instance
What is "mahjong" in the Python library? ??
Is the number equivalent to an integer?
[python] [meta] Is the type of python a type?
In Python, change the behavior of the method depending on how it is called
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
The answer of "1/2" is different between python2 and 3
[Xonsh] The Python shell is sharp and god
What is wheezy in the Docker Python image?
Wagtail is the best CMS for Python! (Perhaps)
I tried the least squares method in Python
To dynamically replace the next method in python
Learn the design pattern "Factory Method" in Python
About the difference between "==" and "is" in python
Python: Prepare a serializer for the class instance:
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
[Python] Predict the appropriate rent for an apartment
This is the only basic review of Python ~ 3 ~
Try implementing the Monte Carlo method in Python
[Python] Seriously think about the M-1 winning method.
An article summarizing the pitfalls addicted to python
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
Usage to call a method of an instance before it is returned by __new__
I thought about why Python self is necessary with the feeling of a Python interpreter
Don't take an instance of a Python exception class directly as an argument to the exception class!