1. 程式人生 > >Python自學之樂-python 2、python 3中經典類、新式類的深度和廣度優先小結

Python自學之樂-python 2、python 3中經典類、新式類的深度和廣度優先小結

python2 __init__ 寫上 print class ast python init 廣度優先

#Author:clark
class Original(object):#在python 3 中寫上object的新式類和不寫的經典類遵循的都是廣度優先原則
def __init__(self):
print("in Original")
class Second(Original):
pass
# def __init__(self):
# print("in the second")
class Third(Original):
def __init__(self):
print("in the third")
class Last(Second,Third):
pass
# def __init__(self):
# print("in the last")

last_obj = Last()
運行結果為:
in the third

如果在python2中經典類結果為
in Original
新式類的結果為
in the third
可以在python2環境中試一下

總結:
python2 中經典類遵循深度優先原則,新式類遵循廣度優先原則
python3 中經典類和新式類都遵循廣度優先原則

Python自學之樂-python 2、python 3中經典類、新式類的深度和廣度優先小結