程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Class in Python

編輯:Python

Father 、 Execution order of subclasses

When a child class inherits a parent class , Subclasses and superclasses have the same functions , So which one to execute first ?

class Animal():
def run(self):
print(" Animals can run !")
class People(Animal):
def run(self):
print(" People can run !")
running = People()
running.run()
"""
Output:
People can run !
"""

When there is __init__() Function ?

#---------------- In the parent class __init__()------------------#
class Animal():
def __init__(self):
print(" The animals began to prepare ...")
def run(self):
print(" Animals can run !")
class People(Animal):
def run(self):
print(" People can run !")
running = People()
running.run()
"""
Output:
The animals began to prepare ...
People can run !
"""
#---------------- Both father and son have __init__()------------------#
class Animal():
def __init__(self):
print(" The animals began to prepare ...")
def run(self):
print(" Animals can run !")
class People(Animal):
def __init__(self):
print(" People begin to prepare ...")
def run(self):
print(" People can run !")
running = People()
running.run()
"""
Output:
People begin to prepare ...
People can run !
"""

It can be seen that , The waves behind the Yangtze River push the waves ahead , Subclasses drown their parents on the beach .....

What about when a subclass inherits multiple parent classes ? Here are two father's chestnuts .

class Animal1():
def __init__(self):
print("1 Start to prepare ...")
def run(self):
print(" animal 1 Can run !")
class Animal2():
def __init__(self):
print("2 Start to prepare ...")
def run(self):
print(" animal 2 Can run !")
class People(Animal1, Animal2):
pass # In order to clearly show the calling order of the parent class , Subclass on pass.
# Of course , If the subclass has output content , It will also give priority to the output of subclass content
"""
Output:
1 Start to prepare ...
animal 1 Can run !
"""
#--------------- Watch an interesting one , Be careful run Function name --------------#
class Animal1():
def __init__(self):
print("1 Start to prepare ...")
def run(self):
print(" animal 1 Can run !")
class Animal2():
def __init__(self):
print("2 Start to prepare ...")
def run2(self):
print(" animal 2 Can run !")
class People(Animal1, Animal2):
pass
"""
Output:
1 Start to prepare ...
animal 2 Can run !
"""

It can be seen that , Subclass People It will be executed according to the order of inheriting the parent class , I'm still closest to my first father . Even if the... Is called ② A father's subfunction , The output of initialization is still the first ① A father .

Take a look at the internal structure ,classname.mro(): Each subclass that inherits from the parent class has a special attribute related to the resolution order of the method ,__mro__ When installing method parsing , Search order of objects , You can use this property to view the execution order of the class . It can be seen that , The order of execution is Start from subclasses → The first parent class → Second parent →...→ The first n A parent class → object.

mro Store the order in tuples , You can access the class by accessing its subscript value , And properties in the class !

People.mro()
"""
Output:
[__main__.People, __main__.Animal1, __main__.Animal2, object]
"""
People.__mro__[2].run2(People)
"""
Output:
animal 2 Can run !
"""
# mro How to use
# classname.mro()
# classname.__mro__
# classname().__class__.mro()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved