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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jun 2 03:12:13 EDT 2015


On Tuesday 02 June 2015 10:24, TheDoctor wrote:

> A type is not an object in the same way an instantiated type is an object
> -- anymore than a blueprint for a building is the building itself.

Nobody says that blueprints are *buildings*. But buildings are not the only 
kind of object that exists.

Plates and spoons and knives and forks are objects.
Cars and trucks and ships and planes are objects.
Books and shoes and maps and compasses are objects.
Buildings are objects.
And blueprints of buildings are objects too.


> The reason this confusion appears is for the exact reason I said on an
> earlier thread:  Python does not define classes in it's grammar

Of course it does. Otherwise "class K: pass" would give you a syntax error.


> -- so it
> can't account for the difference between an [instantiated] object and it's
> definition/type.

Of course it can and does. Python has a rich set of tools for introspecting 
values and determining whether they are instances or classes, what they are 
an instance of, and viewing the differences between the type and the class, 
starting with the humble human-readable string representation:

py> type(23)
<class 'int'>

and going on isinstance, issubclass, vars(), dir(), the inspect module, the 
dis module, and more.

The simplest test to tell whether something is an instance (an object) is:

isinstance(the_thing, object)

and that returns True *always*. The next test is to check whether it is a 
class (a type):

isinstance(the_thing, type)

which returns True only for classes and types. So, classes *are* objects, as 
well as being classes -- just as dogs are mammals, as well as being dogs.



-- 
Steve




More information about the Python-list mailing list