Why is the first argument of [Python] Class self?

Introduction

When defining a class in Python, I've always wondered why the naming of the first argument is `` `self```, so I'll look it up and write it down.

Is it useless except for the first argument self?

From the conclusion, it's no good. In PEP8 (1), always use self as the name of the first argument of the `instance method. Is defined as. However, in terms of operation, it works properly even if it is not `` `self. For an example, refer to here (2).

class Hoge():
    def __init__(self_inplace_hoge, string):
        self_inplace_hoge.string = string
        print('self_inplace_hoge: ', self_inplace_hoge.string)

class Fuga(Hoge):
    def __init__(self_inplace_fuga, string):
        self_inplace_fuga.string = string
        print('self_inplace_fuga: ', self_inplace_fuga.string)

hoge = Hoge('aaaa')
fuga = Fuga('bbbb')

print(hoge.string)
print(fuga.string)
self_inplace_hoge:  aaaa
self_inplace_fuga:  bbbb
aaaa
bbbb

It works, but it's a rule violation, so you can't do it.

By the way, if you look at it as the role of self, it will return where the properly defined class is in memory. Is it fun to look at it unexpectedly?

self_inplace_hoge:  <__main__.Hoge object at 0x00000123D17C1CC8>
self_inplace_fuga:  <__main__.Fuga object at 0x00000123D17C12C8>

at the end

thisAndselfI wonder if anyone could make a rule because if you give it a name you like, it will be dangerous ~ It's kind of like a buzzword.

Reference material

(1) pep8-ja (2) self in Python class

Recommended Posts

Why is the first argument of [Python] Class self?
First Python 3 ~ The beginning of repetition ~
[Python] Which is executed first, the class variable or __init__?
I thought about why Python self is necessary with the feeling of a Python interpreter
[python] [meta] Is the type of python a type?
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
The answer of "1/2" is different between python2 and 3
Why the Python implementation of ISUCON 5 used Bottle
FAQ: Why is the comparison of numbers inconsistent?
This is the only basic review of Python ~ 3 ~
About the * (asterisk) argument of python (and itertools.starmap)
The meaning of self
the zen of Python
Find out the location of Python class definition files.
A python implementation of the Bayesian linear regression class
python (2) requires self because the method is an instance method
[Python] Understand the self of the class. Learn the role of self from the execution result with or without self.
The timing when the value of the default argument is evaluated is different between Ruby and Python.
Don't take an instance of a Python exception class directly as an argument to the exception class!
Towards the retirement of Python2
About the features of Python
The Power of Pandas: Python
Reference order of class variables and instance variables in "self. Class variables" in Python
[Python3] Call by dynamically specifying the keyword argument of the function
What is the default TLS version of the python requests module?
Initial setting of Mac ~ Python (pyenv) installation is the fastest
[Python] The stumbling block of import
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Expansion by argument of python dictionary
March 14th is Pi Day. The story of calculating pi with python
Is the probability of precipitation correct?
"The easiest Python introductory class" modified
[Python] What is @? (About the decorator)
Existence from the viewpoint of Python
[Python] How to get the first and last days of the month
pyenv-change the python version of virtualenv
Why super-intelligents couldn't understand the class
[python] What is the sorted key?
Change the Python version of Homebrew
Get the first element of queryset
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
See python for the first time
Examine the object's class in python
What is the python underscore (_) for?
Science "Is Saito the representative of Saito?"
The first step in Python Matplotlib
If the accuracy of the PCR test is poor, why not repeat the test?
About the basics list of Python basics
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Learn the basics of Python ① Beginners
[Python] What is a formal argument? How to set the initial value
What is the XX file at the root of a popular Python project?
[Python memorandum] Various time verifications ・ The first While statement is abnormally slow
Hit a method of a class instance with the Python Bottle Web API
What kind of book is the best-selling "Python Crash Course" in the world?
The result of making the first thing that works with Python (image recognition)
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?
Generate a first class collection in Python
Explain the mechanism of PEP557 data class