DiamRem
DiamRem

Reputation: 612

Difference between object and instance in Python 2?

This happens in python2.7

I am working on the idea of meta class in python, almost all the tutorial refer object as instance of a class, in python. However, when playing with the class A(): form of defining a class, I saw this:

class ClsDef1():
    pass
C1 = ClsDef1()
print C1
<__main__.ClsDef1 instance at 0x2aea518>

class ClsDef2(object):
    pass
C2 = ClsDef2()
print C2
<__main__.ClsDef2 object at 0x2ae68d0>

This means when create a instance from a class that is not inherent from object, the instance is an instance, but when a class is inherent from object, the instance of the class is an object?

What is the difference? Which should I use?


This question is outdated and refers to a technical distinction that exists in Python 2.x only - referring to how instances of old-style and new-style classes are represented.

In general terminology, the words "instance" and "class" are talking about the same thing. Please see What is the difference between an instance and an object in Python? for a proper explanation.

Upvotes: 20

Views: 21654

Answers (3)

user8310957
user8310957

Reputation: 29

Not unlike the ambiguity between "class" and "type", "instance" is synonymous to "object". Think of it this way: objects are instances of types. So, "42 is an instance of the type int" is equivalent to "42 is an int object". I usually use "instance" and "object" interchangeably. Source for quote: https://eli.thegreenplace.net/2012/03/30/python-objects-types-classes-and-instances-a-glossary

Upvotes: 2

greggo
greggo

Reputation: 3229

Short answer: In python, all objects have a type (returned by type(x)) which is also an object.
if 't' is a type object, then its type is the special type 'type'. So (type(type(x)) is type) is always True. In old classes, a user defined 'class' is a object of the type 'classobj' - and each instance of any class is an object of type 'instance'. I.e. there are two built-in types 'classobj' and 'instance' which implement classes. The linkage from an instance to its class is via its __class__ member.

With new classes: User defined classes are actually new type objects (their type is 'type', not 'classobj') and when you create instances of them, the type() of each instance is the class object. So, objects of different user-defined classes now have distinct types. And classes are on basically the same footing as all builtin types; with old classes there's a separate structure for instance->class and object->type, new classes use object->type for both.

There's much more in the docs, but that's the core of it.

Upvotes: 4

Daniel Roseman
Daniel Roseman

Reputation: 599450

This is the difference between new-style and old-style classes, which is explained in great detail in the documentation. Basically, in Python 2.x you should ensure you always inherit from object so that you get a new-style class. In Python 3, old-style classes have gone away completely.

Upvotes: 12

Related Questions