1. 程式人生 > >python 2 和 python 3的繼承

python 2 和 python 3的繼承

深度 spa init def cnblogs clas 廣度優先 sel utf-8

python 2 和 python 3 代碼均為:

 1 #_*_coding:utf-8_*_
 2 #__author__ = "csy"
 3 
 4 class A:
 5     def __init__(self):
 6         print("A")
 7 
 8 class B(A):
 9     pass
10     #def __init__(self):
11     #    print("B")
12 
13 class C(A):
14     def __init__(self):
15         print("C")
16 
17 class D(B,C):
18 pass 19 #def __init__(self): 20 # print("D") 21 22 d1 = D()

python 2 顯示A,python 3顯示C

python 2的繼承順序是D -> B -> A -> C 深度優先

python 3的繼承順序是D -> B -> C -> A 廣度優先

python 2 和 python 3的繼承