Python 学习 - 经典类和新式类

经典类,不继承object基类,多继承的查找顺序是从左侧深入继承树再往右查找
新式类,继承object基类,多继承的查找顺序是从左往右水平查找,再深入继承树
python2才有新旧之分,python3全是新类

# coding: utf-8


class A:
    def test(self):
        print 'This is A'


class B(A):
    pass


class C(A):
    def test(self):
        print 'this is C'


class D(B,C):
    pass


d = D()
d.test()


结果 This is A

新类

# coding: utf-8


class A(object):
    def test(self):
        print 'This is A'


class B(A):
    pass


class C(A):
    def test(self):
        print 'this is C'


class D(B,C):
    pass


d = D()
d.test()

结果 this is C

猜你喜欢

转载自blog.csdn.net/weixin_38892128/article/details/86545641