what is an object in python ?

In python , we have the class type , and we have the class object .

The class type is used to create a new type/class .

The class type is also used to get the type/class used to create an instance of a type/class .

The class type is also used to get the type/class that was used to create a type/class .

>>> type(object) is type
# type returns the type that was used 
# to create a type or an object .
# the object class/type , was
# created by using the type class
# output
True

>>> type(1) is int
# the int class was used to create 1
# output 
True

>>>type(object()) is object
# the object() instance  was created
# by using the object class
# output 
True 

>>> Aclass = type('Aclass',(object,),{'attribute':1})
# create a new class using the type class
# this new class extends the object class
# and has one property called attribute
# which has a value of 1

>>> type(Aclass) is type
# Aclass was created by using the 
# class type . Every class in python
# is created using the type class 
# and extends the object class
# output
True 

>>> type(Aclass()) is Aclass
# Aclass() instance was created by using
# the Aclass class 
# output 
True

The class type is a subclass of the class object .

>>> issubclass(type,object)
# The issubclass(typeA/classA,typeB/classB) 
# function will return true if typeA is a
# subclass of typeB  , or  if they are the 
# same class or type.
# the class type is a subclass of the class
# object
#  issubclass(type,object) output
True

>>> issubclass(object,object)
# the class object is the class object
# issubclass(object,object) output : 
True
    

Everything is an object in python means :

  • Every type/class that is created in python is a subclass of the class object . This means that every new type that is created in python can implement the methods which are defined in the object class , and can implement its own methods. An instance of this type/class , will have the methods that are implemented .
  • Every type/class that is created , was created by using the object class .
  • Every instance that is created from a new type/class , was also created by using the object class .
>>> issubclass(str , object)
# everythings is an object , means that
# every class/type that is created extends 
# the object class
# issubclass(str , object)
True

>>> issubclass(type,object)
# everything is an object , means also
# that the object class was used to 
# create every class 
# issubclass(type,object) output
True

>>> isinstance(type,object)
# everything is an object ,also means 
# that the object class/type was used to 
# create every instance of a class/type 
# isinstance(type,object) output
True
    
>>> isinstance(1,int)
# 1 is an instance of the int class
# the int class was used to create 1 
# isinstance(1,int) output
True

>>> isinstance(1,object)
# 1 is an instance of the object class
# the object class was used to create 1 
# output isinstance(1,object)
True

>>> isinstance(1,type)
# 1 is not an instance of the type class
# the type class was not used to create 1
# output isinstance(1,type)
False

>>> isinstance(int,object)
# the object class was used to create
# the int class
# isinstance(int,object)
True

>>> isinstance(int,type)
# the type class is used to create
# every type/class , as such it was used 
# to create the int type/class
# isinstance(int,type)
True

>>> isinstance(object,type)
# the type class was used to create
# the object class
True
    
    

An object in python can also be viewed as being : abstraction of data . So we abstract the methods and properties that data can have . Programming code is itself data , meaning we can abstract its properties and operations . For example in a programming code we have the characters that make the code , we have the syntax , and the operations –functions , operators — that we can perform using this code .

So in python we can think of data or objects which are the abstraction of data as

object : {type , [attributes] , [methods]}

Type for example the programming code , will specify the attributes and methods that its object will have.