Difference between class declarations

前端 未结 3 880
一向
一向 2021-01-20 21:20

I see some similar questions about this topic, but i wish to be sure, so i am asking...

What is the difference between:

class MyClass:
    pass
         


        
3条回答
  •  野的像风
    2021-01-20 21:53

    There is no difference between the first two spellings.

    In python 2.7, there is a huge difference between the latter two. Inheriting from object makes it a new-style class, changing the inheritance semantics and adding support for descriptors (@property, @classmethod, etc.). It's the default in Python 3.

    New-style classes were introduced in Python 2.2 to unify types (such as int and list), and classes, and because several things change in backwards-incompatible ways, you need to 'opt in', explicitly inherit from object to enable the changes.

    In Python 3, inheriting from object is no longer needed, classes are new-style, always.

提交回复
热议问题