Python class inherits object

后端 未结 6 1041
慢半拍i
慢半拍i 2020-11-21 15:54

Is there any reason for a class declaration to inherit from object?

I just found some code that does this and I can\'t find a good reason why.



        
6条回答
  •  广开言路
    2020-11-21 16:33

    The syntax of the class creation statement:

    class (superclass):
        #code follows
    

    In the absence of any other superclasses that you specifically want to inherit from, the superclass should always be object, which is the root of all classes in Python.

    object is technically the root of "new-style" classes in Python. But the new-style classes today are as good as being the only style of classes.

    But, if you don't explicitly use the word object when creating classes, then as others mentioned, Python 3.x implicitly inherits from the object superclass. But I guess explicit is always better than implicit (hell)

    Reference

提交回复
热议问题