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

Steven D'Aprano steve at pearwood.info
Mon Jun 1 00:24:24 EDT 2015


On Mon, 1 Jun 2015 01:09 pm, Eddilbert Macharia wrote:

> Thank you for you responses guys.
> 
> So what I'm getting from above responses, everything in python is an
> object because the are instance of the metaclass type (from Steven
> response) and also because they are subclasses of the class object (from
> Marco and Terry response)? 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.

Neither. You have the relationship confused.

Everything is an *instance* of the *class*, not the metaclass:

py> isinstance(42, type)  # type is the metaclass
False
py> isinstance(42, int)  # int is the class of 42
True


Most things are *not* subclasses of object:

py> issubclass(42, object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class



> if its because of being subclass of object class, does that mean if the
> class object was named class pyobj. would that make everything python
> pyobj ?

No. "Object" is a standard term from computer science, Python could
rename "object" to "wibble", and we would still say "everything is an
object.

There are *two* (actually three) common meanings for the word "object" in
Python programming:

- the general meaning of object, as in Object Oriented Programming, shared
with hundreds of other languages;

- the specific, Python meaning, as in the built-in class called "object";

- and more rarely, as a synonym for "instance", as opposed to "class".


"Everything is an object" is understood in terms of Object Oriented
Programming. It was true way back in Python version 1.5, before the
built-in "object" class existed. It would still be true even if the
built-in "object" class was renamed to "wibble".

This:

py> isinstance(23, object)
True

is true because "object" is also the base class for everything in Python.
But that's a separate meaning.



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

No. type and object are special, and have to be bootstrapped into existence
by the interpreter. You cannot create type first, because type is an
object, so object needs to exist first. But you cannot create object first,
because object is a type, and so type has to exist.

You cannot solve this problem by creating one first, then the other. The
interpreter has to create them both.

[..]
> will i ever get this or is it a chicken and egg situation ?

Yes, exactly. You cannot break the cycle from Python code, it has to be done
deep inside the interpreter.




-- 
Steven




More information about the Python-list mailing list