What is the difference between old style and new style classes in Python?

前端 未结 8 1315
Happy的楠姐
Happy的楠姐 2020-11-21 04:48

What is the difference between old style and new style classes in Python? When should I use one or the other?

8条回答
  •  余生分开走
    2020-11-21 05:33

    Important behavior changes between old and new style classes

    • super added
    • MRO changed (explained below)
    • descriptors added
    • new style class objects cannot be raised unless derived from Exception (example below)
    • __slots__ added

    MRO (Method Resolution Order) changed

    It was mentioned in other answers, but here goes a concrete example of the difference between classic MRO and C3 MRO (used in new style classes).

    The question is the order in which attributes (which include methods and member variables) are searched for in multiple inheritance.

    Classic classes do a depth-first search from left to right. Stop on the first match. They do not have the __mro__ attribute.

    class C: i = 0
    class C1(C): pass
    class C2(C): i = 2
    class C12(C1, C2): pass
    class C21(C2, C1): pass
    
    assert C12().i == 0
    assert C21().i == 2
    
    try:
        C12.__mro__
    except AttributeError:
        pass
    else:
        assert False
    

    New-style classes MRO is more complicated to synthesize in a single English sentence. It is explained in detail here. One of its properties is that a base class is only searched for once all its derived classes have been. They have the __mro__ attribute which shows the search order.

    class C(object): i = 0
    class C1(C): pass
    class C2(C): i = 2
    class C12(C1, C2): pass
    class C21(C2, C1): pass
    
    assert C12().i == 2
    assert C21().i == 2
    
    assert C12.__mro__ == (C12, C1, C2, C, object)
    assert C21.__mro__ == (C21, C2, C1, C, object)
    

    New style class objects cannot be raised unless derived from Exception

    Around Python 2.5 many classes could be raised, and around Python 2.6 this was removed. On Python 2.7.3:

    # OK, old:
    class Old: pass
    try:
        raise Old()
    except Old:
        pass
    else:
        assert False
    
    # TypeError, new not derived from `Exception`.
    class New(object): pass
    try:
        raise New()
    except TypeError:
        pass
    else:
        assert False
    
    # OK, derived from `Exception`.
    class New(Exception): pass
    try:
        raise New()
    except New:
        pass
    else:
        assert False
    
    # `'str'` is a new style object, so you can't raise it:
    try:
        raise 'str'
    except TypeError:
        pass
    else:
        assert False
    

提交回复
热议问题