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

Eddilbert Macharia edd.cowan at gmail.com
Mon Jun 1 08:03:23 EDT 2015


Thank you all who have taken there time to assist me.
Special thanks to Steven D'Aparo even though you didn't have two you have taken you time to go into details.

I think i kind of understand now. 

Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types

In some context type means the data type an object is e.g. an int,string e.t.c.

In the new class style, type and class sort of mean the same thing.

**type type is used to create new types when used as a metaclass 
**type object is used to provide some default behavior and attributes to this types through inheritance

The interpreter makes sure of the following 

** The type type inherits from data type object which makes the following statements are true

>>> issubclass(type,object)
True # type inherits object as its parent

>>> isinstance(type,object) 
True # because of the inheritance 

>>> type.__bases__ 
(<class 'object'>,) # because of the inheritance 

>>> isinstance(type,type) 
True # because metaclass type was used to create this data type type

** The type object is created using the type type i.e. type object is an instance of type type. The type object does not subclass i.e. inherit from any other types. 

>>> isinstance(object,type) 
True # because metaclass type was used to create this object

>>> issubclass(object,type)
False # because object does not inherit from any other class

>>> object.__bases__
() # because object does not inherit from any other class



On Sunday, May 31, 2015 at 5:34:20 PM UTC+3, Eddilbert Macharia wrote:
> Hello All ,
> 
> I'm wrecking my head trying to understand. where the class object comes into play . 
> 
> Is it only meant to act as base class and does it mean there is an actual class called object in python which all the objects created using the class type inherit ?
> 
> i'm assuming the metaclass if simplified would look something like this :
> 
> type('dict', (object,),{})
> 
> And when we use the class type as a metaclass are we using the instance version of the class type or are we actually using the type class itself ?
> 
> 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 ? 
> 
> As can be attested by using type() function as below :
> 
> >>> type(int)
> <class 'type'>
> >>> type(list)
> <class 'type'>
> >>> type(dict)
> <class 'type'>
> >>> type(type)
> <class 'type'>
> >>> type(object)
> <class 'type'>
> 
> 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.
> 
> Now if i look at the __bases__ of all this objects i get :
> 
> >>> type.__base__
> <class 'object'>
> >>> type.__bases__
> (<class 'object'>,)
> >>> dict.__bases__
> (<class 'object'>,)
> >>> list.__bases__
> (<class 'object'>,)
> >>> int.__bases__
> (<class 'object'>,)
> >>> object.__bases__
> ()
> 
> This tells me that all of this objects inherit from the class object which has nothing to do with them being instances.




More information about the Python-list mailing list