Anyone who started python wondered, Content that is full of content everywhere.
Reorganize for yourself.
・ Self is optional └ Works even if it is not self
-When using a variable in the class, use the self. Variable. └ If you use x = 123, "self.x"
・ Global variables can be used as they are in the class
-[Difference in behavior with and without self](Difference in behavior with and without #self) ・ [It doesn't have to be self](It doesn't have to be #self) -[Differences inside and outside the def class](Differences inside and outside the #def class) -[Set arguments with methods in class](#Set arguments with methods in class) -[Use variables in class](# Use variables in class)
** ▼ Confirmation class **
class1
class class1:
def method1(self):
print("Executed method1")
x = class1()
x.method1()
#output
#Executed method1
No self
class class1:
def method1():
print("Executed method1")
x = class1()
x.method1()
#output
# TypeError: method1() takes 0 positional arguments but 1 was given
** ■ Error ** method1() takes 0 positional arguments but 1 was given
The method "method1" defined in the class does not take an argument (0), but 1 is given.
Even in the executed process, the inside of () is empty and no argument is entered. (Normally argument 0) x.method1()
Example
x.method1 () → Argument 1
x.method1 ('A','B') → Argument 3
⇛ Its argument is "self"
The argument may be other than self. It is customary to use self.
When the argument self is replaced with "a" and "aiueo".
Characters other than self ①
class class1:
def method1(a):
print("Executed method1")
x = class1()
x.method1()
#output
#Executed method1
Characters other than self ②
class class1:
def method1(AIUEO):
print("Executed method1")
x = class1()
x.method1()
#output
#Executed method1
Both run without problems.
If you bring the defined method out of the class, of course no arguments are passed by default.
Out of class
def method1():
print("Executed method1")
method1()
#output
#Executed method1
error
def method1():
print("Executed method1")
method1('A')
#output
# TypeError: method1() takes 0 positional arguments but 1 was given
Error: method1 () takes 0 positional arguments but 1 was given
An error that there are 0 arguments but 1 extra.
Since one argument is set by default in the class, when setting other arguments, it is necessary to set one more than usual. (Becomes self + α)
When setting arguments
class class2:
def weather(self, day, sky):
print(day + 'The weather is' + sky + 'is.')
x = class2()
x.weather('today', 'Sunny')
#output
#The weather is sunny today.
Set three arguments of the weather method in the class: "self", "day", and "sky".
Other than self
class class2:
def weather(a, day, sky):
print(day + 'The weather is' + sky + 'is.')
x = class2()
x.weather('today', 'Sunny')
#output
#The weather is sunny today.
The big advantage of a class is that you can set functions and variables that are valid only within the class. (Not global)
You need to combine it with "self" to use variables in the class.
To call the variable "x = 123" defined in the class in the method, it must be "self.x".
class3
class class3:
x = 123
def method1(self):
print(self.x)
cls = class3()
cls.method1()
#output
# 123
If there is no self in "self.x", an error will occur.
x only
class class3:
x = 123
def method1(self):
print(x)
cls = class3()
cls.method1()
#output
# NameError: name 'x' is not defined
Error: name'x' is not defined "X = 123" in the class is not "x".
If you use a variable defined outside of class, you don't need self.
Use global variables
x = 123
class class3:
def method1(self):
print(x)
cls = class3()
cls.method1()
#output
# 123
By using self, you can clearly distinguish whether it is a global variable or a variable in a class.
Other than self
class class3:
x = 123
def method1(AIUEO):
print(AIUEO.x)
cls = class3()
cls.method1()
#output
# 123
I was wondering what self is, but I think it's convenient and necessary to know how to use it.
Recommended Posts