Review the basic operations of Python only for the parts that you are uneasy about. As the third step, we describe object orientation and classes.
In short, it handles data and operations as a group. It's like putting variables and functions together. The group is called ** object **.
In object orientation The processing target is divided into object units, and messages are exchanged with each other to proceed with processing.
Places where maintainability can be expected, such as minimizing the impact of corrections
In object-oriented programming, there is a mechanism that allows the data inside an object to be accessed only by using a prepared operation, which is called encapsulation. The image is shown below.
In Fig. 1, the processing represents the processing from outside the object, and each blue frame represents the object. It can be seen that there are data and operation elements inside the object, and the data cannot be accessed directly from the outside. However, you can access the data by stepping on the operation.
Object: A collection of data and operations Class: Something like an object blueprint Instance: A materialized object
In the following, we will check with the actual code.
class.py
"""
2020/12/14
@ Yuya Shimizu
class
"""
#Definition of a class called User
class User:
#constructor
def __init__(self, name, password):
self.name = name
self.password = password
#Login method
def login(self, password):
if self.password == password:
return True
else:
return False
#success method
def success(self):
print('User: ' + self.name + ' --- login successfully !!')
#failure method
def failure(self):
print('User: ' + self.name + ' --- sorry, password is not correct')
#Instantiate to a
a = User('Yuya', 'PassWord')
user_input = input('\nplease input your password\n>>')
if a.login(user_input):
a.success()
else:
a.failure()
class~
Design an object with
Object name()
Instantiate with
By specifying the operation for the instance, the object is accessed.
class.py
"""
2020/12/14
@ Yuya Shimizu
class
"""
#Definition of a class called User
class User:
#constructor
def __init__(self, name, password):
self.name = name
self.password = password
#Login method
def login(self, password):
if self.password == password:
return True
else:
return False
#success method
def success(self):
print('User: ' + self.name + ' --- login successfully !!')
#failure method
def failure(self):
print('User: ' + self.name + ' --- sorry, password is not correct')
#Instantiate to a
a = User('Yuya', 'PassWord')
user_input = input('\nplease input your password\n>>')
if a.login(user_input):
a.success()
else:
a.failure()
#Class inheritance
class GuestUser(User):
def __init__(self):
super().__init__('guest', 'guest')
guest = GuestUser()
user_input = input('\nGuest, please input your password\n>>')
if guest.login(user_input):
guest.success()
else:
guest.failure()
There is something called inheritance in class. A parent class is defined, and the same structure as the parent class can be inherited by the child class. Only the inherited part is shown below.
#Class inheritance
class GuestUser(User):
def __init__(self):
super().__init__('guest', 'guest')
There is a parent class as an argument after the object name, and it is also possible to change the argument in the class __init __ ()` ``. It is executed by
super () .__ init __ ()` ``.
Not from the conclusion.
However, instead, there is a rule that variables and methods starting with __`` and` `__
are not referenced from the outside, and an error occurs when accessing them. Actually verify.
rule_.py
"""
2020/12/14
@ Yuya Shimizu
class
Underscore rules
"""
#Definition of a class called User
class User:
#constructor
def __init__(self, name, password):
self.name = name
self.password = password
self.__password = password
#Instantiate to a
a = User('Yuya', 'PassWord')
print(a.name)
print(a.password)
print(a.__password)
The execution result is shown below.
Yuya
PassWord
Traceback (most recent call last):
File "D:/Python Skills/Basic/Object-oriented and class/rules__.py", line 25, in <module>
print(a.__password)
AttributeError: 'User' object has no attribute '__password'
From the execution result, it can be seen that an error has occurred only in the part of ``` print (a.__ password)
, and it cannot be accessed just by adding
__
.
I've used the class several times, but I finally understood how it works and was convinced. I didn't seem to know the rules of underscores, so I'm glad I could understand them here.
Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha
Recommended Posts