6

When class is inherited from nothing, I have an object of instance type.

>>> class A(): pass;
>>> a = A()
>>> type(a)
<type 'instance'>
>>> type(a) is A
False
>>> type(A)
<type 'classobj'>

However, when I have the same class inheriting from an object, the created object is type of A.

>>> class A(object): pass;
>>> a = A()
>>> type(a)
<class '__main__.A'>
>>> type(a) is A
True
>>> type(A)
<type 'type'>

What is the logic behind this? Does this mean every class should inherit from object?

arshajii
  • 118,519
  • 22
  • 219
  • 270
prosseek
  • 155,475
  • 189
  • 518
  • 818

3 Answers3

8

In Python 3, those two are the same. In Python 2, however:

class A: pass  # old-style class

class B(object): pass  # new-style class

From New-style and classic classes in the documentation:

Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.

New-style classes were introduced in Python 2.2 to unify classes and types. A new-style class is neither more nor less than a user-defined type. If x is an instance of a new-style class, then type(x) is the same as x.__class__.

The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model. It also has a number of immediate benefits, like the ability to subclass most built-in types, or the introduction of "descriptors", which enable computed properties.

For these reasons, it's a good idea to use new-style classes whenever you can. The only reason old-style classes even exist in Python 2.2+ is for backwards compatibility; in Python 3, old-style classes were removed.

Community
  • 1
  • 1
arshajii
  • 118,519
  • 22
  • 219
  • 270
  • 1
    Nice answer. I was going to suggest adding something like "The only reason ever to use old-style classes is when you're calling into some ancient library that requires you to give it `instance`s", but your last edit covered that nicely. – abarnert Aug 20 '13 at 22:33
1

The original implementation of user-defined classes in Python sucked. 2.2 fixed it, but they had to keep the old system around for backward compatibility. Thus, Python has 2 types of classes, "classic" and "new-style". Anything that inherits from object (directly or indirectly) is new-style; anything that doesn't is classic. Every class you write should inherit from object in some way, either directly if it doesn't have any other parents, or indirectly because it inherits from another new-style class (or a built-in type).

See http://python-history.blogspot.com/2010/06/new-style-classes.html

user2357112 supports Monica
  • 215,440
  • 22
  • 321
  • 400
0

this explains the instance thing:

>>> class A(): pass;
>>> A
<class __main__.A at 0x7f879998b050>
>>> A()                                            
<__main__.A instance at 0x7f87999a83b0>

but you shouldn't be using type() to compare objects in python anyways, you should be using isinstance()

Cameron Sparr
  • 3,476
  • 2
  • 17
  • 28