Base class base of the new Python style - `object` and` type`

According to the document object, this is the base class class of the new style.

And AFAIK, the so-called new-style classes are just those that can acquire some new bindings by inheriting object, right?

I thought that I should objectinherit typeor use typeit __metaclass__, but object.__bases__it doesn’t give anything, therefore, where does this come from object, and what kind of relationship does it mean andtype

+5
source share
3 answers

Indeed, the type (ie metaclass) object, class type:

type(object) == type      # True

And since it objectis a base class, it does not have its own parents, as you would expect:

object.__bases__ == ()    # True

object __metaclass__, : , type.

, type object, ( type object, type object?), C Python.

, , object. Python 3 , Python 3.

+5

, :

  • python - ,
  • - , , __bases__.

, object type .

, "" - type, , , ( - , ).

C voodoo python, !

EDIT: ( )

>>> class MyMeta(type):
...     def __new__(cls, name, bases, dct):
...         return type.__new__(cls, name, bases, dct)
... 
>>> class MyClass(object):
...     __metaclass__ = MyMeta
... 

, obj object

>>> obj = MyClass()
>>> MyClass.__bases__
(<type 'object'>,)

dir(obj), __metaclass__: , __metaclass__ , . , :

>>> dir(MyClass)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> MyClass.__metaclass__
<class '__main__.MyMeta'>

, SO ( , !):

Python?

!

+3

You can find this and this post is interesting. Here is a chart from the first:

enter image description here

+2
source

All Articles