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.
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>
this
Andself
I 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.
(1) pep8-ja (2) self in Python class