Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between object and instance in python?

Tags:

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?

Could anyone explain the difference? In real life which one should I use?

Thanks!

like image 331
DiamRem Avatar asked May 25 '12 17:05

DiamRem


People also ask

What is the difference between instance and object?

Instance refers to Reference of an object. Object is actually pointing to memory address of that instance. A single object can have more than one instance. Instance will have the both class definition and the object definition where as in object it will have only the object definition.

What is a instance in Python?

In the Python programming language, an instance of a class is also called an object. The call will comprise both data members and methods and will be accessed by an object of that class.

What is class object and instance in Python?

A class is a code template for creating objects. Objects have member variables and have behaviour associated with them. In python a class is created by the keyword class . An object is created using the constructor of the class. This object will then be called the instance of the class.

Why is an object called an instance?

An object that is created using a class is said to be an instance of that class. We will sometimes say that the object belongs to the class. The variables that the object contains are called instance variables. The methods (that is, subroutines) that the object contains are called instance methods.


2 Answers

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.

like image 21
greggo Avatar answered Nov 11 '22 23:11

greggo


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.

like image 156
Daniel Roseman Avatar answered Nov 12 '22 00:11

Daniel Roseman