Object Oriented and [Object Oriented Programming](http://e-words.jp/w/E382AAE38396E382B8E382A7E382AFE38388E68C87E59091E382AFE38388E68C87E59091E38397E It's an essential concept. All data in a Python program is represented as objects or relationships between objects. All the strings and lists that have been described so far are objects.
However, this page does not provide a "plain" and "accurate" explanation of object orientation. Therefore, even in the explanation of strings and lists, I have not dared to explain the part about objects (and methods). The rest of this article will explain how to use classes and objects in Python, assuming you understand object orientation.
The program that actually uses the class and the object is written below. Please execute it and check the operation.
class_explain.py
class Spam:
val = 100
def ham(self):
self.egg('call method')
def egg(self,msg):
print("{0}".format(msg))
print(("{0}".format(self.val)))
spam = Spam()
spam.ham()
The result is as follows.
call method 100
In Python, classes are created with ** class ** * class name ***: **. The class name starts with an uppercase letter. Class variables and in-class functions (= methods) are defined using indentation. Objects are created with * object * ** = ** * class () *. The method is called with * object.method () *. If you explain the above program in Japanese
The point is the first argument ** self ** of the method. For those who entered Python from other languages You might wonder, "What is this self when spam.ham () has no arguments?" In Python, methods have at least one argument. It is customary to always name this first argument self. By using self, you can get the variable of the object itself or call the method. It is "this" in Java.
A special function that is called when an object is created is called a constructor.
Initialization of the data handled by the object is performed here.
The constructor is defined with the name __init __ ()
. There are two " _
"before and after. be careful.
constructor_explain.py
class Spam:
def __init__(self,ham,egg):
self.ham = ham
self.egg = egg
def output(self):
sum = self.ham + self.egg
print("{0}".format(sum))
spam = Spam(5,10)
spam.output()
The result is as follows.
15
The opposite of the constructor, this function is automatically executed when the object is no longer needed and Python deletes it.
Define it with a method named __del__
. But in most cases you don't define a destructor. As a reason
We use with to free resources, but we won't explain it here because it goes beyond the level.
"Inheritance" is defining one class as a class with data and methods from another class. Inheritance is an important idea common to object-oriented programming languages as well as Python. Please execute the following program.
extend_explain.py
class Base:
basevalue = "base"
def spam(self):
print("Base.spam()")
def ham(self):
print("ham")
class Derived(Base):
def spam(self):
print ("Derived.spam()")
self.ham()
derived = Derived()
print("{0}".format(derived.basevalue))
derived.ham()
** class ** * class name (base class name) * **: ** allows you to define a class (derived class) that inherits the variables and methods of the base class. The derived class inherits the variables and methods of the base class, so the output looks like this:
base ham
Python allows "multiple inheritance" to inherit multiple base classes. ** class ** * Class name (base class name 1, base class name 2, ...) *
multi_extend.py
class A:
def method(self):
print("class A")
class B:
def method(self):
print("class B")
class C(A):
def method(self):
print("class C")
class D(B,C):
pass
d = D()
d.method()
The output result will be ** class B **. Although rarely used, you can create a class that does nothing by writing pass in the class. When you call the method method () of the object d of class D, you can use the base classes B and C of class D. The class B method method () described earlier is called and ** class B ** is output. If class B of the above program is described only by pass like class D, the method method () does not exist in the base class B. The class C method method () is called and the output is ** class C **. If only pass is described in class C, the method method () of class A, which is the base class of class C, will be called. ** class A ** is output. If class A is described only as pass, an exception will be thrown because method () does not exist in any class.
Creating complex multiple inheritance can result in an error because the method search order cannot be determined. be careful.
Encapsulation is an important element in object-oriented programming. In Python, you can make a variable or method private by prepending "\ _ \ _" (two _). Please execute the following program.
private_explain.py
class Spam:
__attr = 100
def __init__(self):
self.__attr = 999
def method(self):
self.__method()
def __method(self):
print(self.__attr)
spam =Spam()
spam.method() #OK
spam.__method() #NG
spam.__attr #NG
The spam.method () line runs fine, but the spam. \ _ \ _ Method () line gives an error. The method method of class Spam is public because it doesn't start with "\ _ \ _". It can be called directly from the object. I'm calling \ _ \ _method () with "\ _ \ _" at the beginning in the method method, Since it is a reference from the inside, no error will occur. But if the spam object tries to call \ _ \ _method () directly, \ _ \ _ Method () with "\ _ \ _" at the beginning cannot be called because it is private, and an error will occur. This also applies to spam. \ _ \ _ Attr.
Next: Python Basic Course (14 Modules and Packages)
Recommended Posts