Basic knowledge of Python ④. It is my study memo. Please do not excessive expectations.
Python basics Python basics ② Python basics ③
-Mechanism to generate "things"
In order to generate the "thing", it is first necessary to prepare the "blueprint".
The blueprint is class
,
"Things" are called instances
.
1, "Prepare a class"
--Class is defined by " class class name: "
Example
class Vehicletype: #Colon required at end of line
pass #Indicates that there is no processing
#Align the indents (4 half-width spaces)
2, "Create an instance from a class"
-With " class name () "
and calling that class
You can create a new instance using the class.
Then, by setting "variable name = class name ()", the created instance can be assigned to the variable.
Example
class Vehicletype: #Colon required at end of line
pass #Indicates that there is no processing
#Align the indents (4 half-width spaces)
vehicle_type1 = Vehicletype()
#Instantiate from Vehicletype class
3, add information to the instance
-By setting "vehicle_type1.name =" track'", it becomes menu_item1
You can add information that the "name" is a "track".
At this time, "name" is called " instance variable "
.
Example
class Vehicletype: #Colon required at end of line
pass #Indicates that there is no processing
#Align the indents (4 half-width spaces)
vehicle_type1 = Vehicletype()
#Instantiate from Vehicletype class
vehicle_type1.name = 'truck'
print(vehicle_type1.name)
#Output result → Track
vehicle_type2 = Vehicletype()
#Instantiate from Vehicletype class
vehicle_type2.name = 'bus'
print(vehicle_type2.name)
#Output result → Bus
-Functions can be defined in the class.
The function defined in the class is called method
.
-How to define a method
You need to add self to the first argument
Example
class Vehicletype:
def hello(self): #Add self to the first argument
#Align the indents (4 half-width spaces)
print('Hello')
Example
class Vehicletype:
def hello(self): #Add self to the first argument
#Align the indents (4 half-width spaces)
print('Hello')
#Align the indents (4 half-width spaces)
vehicle_type1 = Vehicletype()
#Instantiate from Vehicletype class
vehicle_type1.hello()
#Output result → Hello
-The instance that called the method is assigned to "self" specified in the first argument of the instance method. Therefore, by setting "self.name", the value of "name" of "vehicle_type1" that calls the method can be obtained.
Example
class Vehicletype:
def info(self): #self to vehicle_type1 is assigned
print(self.name)
vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'
vehicle_type1.info
#Output result → Track
-Can be returned as a return value using "return"
Example
class Vehicletype:
def info(self): #self to vehicle_type1 is assigned
return self.name
vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'
print(vehicle_type1.info )
#Output result → Track
-Instance method (argument)
Example
class Vehicletype:
def info(self, count): #"4" is assigned to count, the order of the arguments shifts by the amount of "self"
vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'
vehicle_type1.info(4)
-__ init__ method
The __init__ method
is a method that is automatically called immediately after creating an instance with" class name () ".
Example
class Vehicletype:
def __init__(self):
print('Please select a car model')
vehicle_type1 = Vehicletype()
#Since the instance was created__init__The method is called automatically
#Output result → Please select a model
Example
class Vehicletype:
def __init__(self):
self.name = 'truck'
vehicle_type1 = Vehicletype()
#Since the instance was created__init__The method is called automatically
print(vehicle_type1.name)
#Output result → Track
-__ init__ method
(argument)
Like instance methods, it can also take arguments.
Example
class Vehicletype:
def __init__(self, name):
self.name = name
vehicle_type1 = Vehicletype('truck')
#Since the instance was created__init__The method is called automatically
print(vehicle_type1.name)
#Output result → Track
Recommended Posts