5

According to the doc, object is all new-style classes' base class.

And AFAIK, the so-called new-style classes are just ones which can acquire some new fetures by inheriting object, right?

I thought object inherit type or use type as its __metaclass__, but object.__bases__ gives me nothing, so where dose this object come from, and what's relationship bewteen it and type?

Alcott
  • 17,905
  • 32
  • 116
  • 173

3 Answers3

5

Indeed, the type (i.e., metaclass) of object, a class, is type:

type(object) == type      # True

And since object is the base class, it has no parents of its own, as you'd expect:

object.__bases__ == ()    # True

object doesn't have a __metaclass__ attribute because it doesn't need one: it uses the default metaclass, type.

Now it's a little confusing because type is in fact a subclass of object, which boggles the mind (how can type be derived from object when you need type to construct object?) but this is solved by a little hard-coding at the C level in the Python interpreter.

All this applies only to new-style classes, that is, those derived from object. In Python 3, all classes are new-style, so this applies globally in Python 3.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    use the default metaclass? Do you mean, if I define a class (not inheriting `object`) without specifying its metaclass, then this class will be construted by `type`? – Alcott May 08 '12 at 06:43
  • 1
    Yes. `type` is the metaclass Python uses if you don't specify a metaclass. – kindall May 08 '12 at 06:45
  • 1
    If you don't inherit from object, you will get an old-style class. If you're in Python 3, not inheriting from anything is the same as inheriting from object. – yak May 08 '12 at 06:48
  • 1
    @yak, so in 3.0, all classes (inherit `object` or not ) are all the same? – Alcott May 08 '12 at 06:51
  • 1
    Yes. `class A:`, `class A():`, `class A(object):` are all the same in 3. – yak May 08 '12 at 06:54
  • 1
    @yak, then why not just abandon `object` in 3.0 ? – Alcott May 08 '12 at 06:58
  • 1
    Because there must be some base class, for example to provide default implementation of magic methods like `__setattr__` or `__repr__`. You see `class A(object):` in Python code a lot because it is the form that gives you a new-style class in any Python version since 2.2 iirc. – yak May 08 '12 at 07:05
3

There are two concepts that could be helpful to keep in mind:

  • everything in python is an object, including classes and metaclasses
  • metaclasses are the constructors of classes, not their ancestors, so they do not appear in __bases__.

That means that object indeed has type as metaclass.

Just as a curiosity, the "paradoxical" part of the story is the type, which being a metaclass is also an object, but can't have itself as metaclass (it would be a bit of chicken-and-egg problem, if you think about it).

The paradox is solved with some C voodoo in the python source code, but I don't know much about it!

EDIT: (some example code)

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

Now observe that obj inherit from object

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

As for your question in the comments about dir(obj) doesn't output the __metaclass__ attribute: the reason is that __metaclass__ is an attribute of the class not of its instantiated object. Note in fact that:

>>> 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'>

If you are interested in deepening your understanding of metaclasses, this is a classic SO question (with a very comprehensive answer, of course!):

What is a metaclass in Python?

HTH!

Community
  • 1
  • 1
mac
  • 42,153
  • 26
  • 121
  • 131
  • Well, I actually think that `type` is `object`'s metaclass, but `dir(object)` doesn't even have an attribute as `__metaclass__`, why? – Alcott May 08 '12 at 06:41
  • 1
    Because `type` is used by default. `__metaclass__` can be used to override it. – yak May 08 '12 at 06:50
  • @Alcott - no worries. yak is spot-on with his remark. I'm adding one more link to my answer that should help you understanding metaclasses more if you are interested. :) – mac May 08 '12 at 06:56
2

You may find this and this posts interesting. Here's the diagram from the first:

enter image description here

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412