Everything is an object in python - object class and type class

Marco Buttu marco.buttu at gmail.com
Sun May 31 11:13:20 EDT 2015


On 31/05/2015 16:34, Eddilbert Macharia wrote:

> Also when we say everything is an object in python, are we referring to the fact that everything is an instance of the class type or does it have to with the object class inherited ?

 From Wikipedia: "In the class-based object-oriented programming 
paradigm, `object` refers to a particular instance of a class"

So, because in Python everything is an instance of the class object 
(isinstance(obj, object) is always True), everything is an object.
Only classes are instance of type. That means is worth to differentiate 
between classes and non-classes, but it is a non-sense to differentiate 
between classes and objects. If it could be useful:

http://marco-buttu.github.io/pycon_objmodel/

> As can be attested by using type() function as below :
>
>>>> >>>type(int)
> <class 'type'>
>>>> >>>type(list)
...
> >From my understanding this means all of this are instances of the 
> class type. which means the class type was used to create this instances. 

Yes, that is true, because all these objects are classes

> Now if i look at the __bases__ of all this objects i get :
>>>> >>>type.__base__
> <class 'object'>
>>>> >>>type.__bases__
> (<class 'object'>,)
> ...

> This tells me that all of this objects inherit from the class object which has nothing to do with them being instances.

Take a look at all the steps that anticipate this diagram:

http://marco-buttu.github.io/pycon_objmodel/?full#Diagram

Every class inherits from object, and that implies every instance of a 
class is an instance of object. And classes are instance of a class 
(thier metaclass), so they are instance of objects too

-- 
Marco Buttu

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbuttu at oa-cagliari.inaf.it




More information about the Python-list mailing list