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

前端 未结 8 1309
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:31

    Declaration-wise:

    New-style classes inherit from object, or from another new-style class.

    class NewStyleClass(object):
        pass
    
    class AnotherNewStyleClass(NewStyleClass):
        pass
    

    Old-style classes don't.

    class OldStyleClass():
        pass
    

    Python 3 Note:

    Python 3 doesn't support old style classes, so either form noted above results in a new-style class.

提交回复
热议问题