python type class and object class

17 views
Skip to first unread message

Eddilbert Macharia

unread,
May 31, 2015, 11:28:21 PM5/31/15
to django...@googlegroups.com
Hello All,

I know this is not python group but i believe you guys can help, especially because Django makes heavy use of metaclases.

I'm having headache trying to understand the cyclic relationship that exit between the `metaclass` type, the `object` class, and the `class` type.

I'm trying to understand how python makes everything an object.is it because everything is an instance of the metaclass type or is it because everything is a subclass of object class.

if its because of being subclass of object class, does that mean if the class object was named class `pyobj`. Would that mean that everything in Python starts with pyobj?

I know objects created by metaclass are types/classes, this types are then used to create other object.

From this:

    >>> isinstance(type, object)
    True
    >>> isinstance(object,type)
    True
    >>> issubclass(object,type)
    False
    >>> issubclass(type,object)
    True

Is it safe to say that python creates the class object first using the type metaclass (I'm simplifying the metaclass for brevity).

    type('object',(),{})

which implies class object is a class of class type and it does not inherit any attributes other class.

Then it creates the class type:

    type('type', (object,),{})

implying type class is class of class type and it inherits attributes from the object class.

Then creates the other classes by inheriting from the class object

    type('dict', (object,), {}) 
    type('Animal', (object), {})

which similar to creating an Animal class as :
   
    class Animal:
         pass

Does this mean the metaclass used to create the class object is still the one used to create this Animal class or is the metaclass type used by default ?

Which type is being used, is it the metaclass type or the type class that was created after object was created ?

Where does the class type created with the base class object come into play ?

I have also tried to understand what really is going on between the object and the class from all he responses above and in this article http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

I'm still getting confused. What is the relation between this two class in terms of object creation?

Will I ever get this or is it a chicken and egg situation?
Reply all
Reply to author
Forward
0 new messages