PyWart (Terminolgy): "Class"

Steven D'Aprano steve+comp.lang.python at
Tue Jan 15 10:01:41 CET 2013

On Mon, 14 Jan 2013 22:54:10 -0800, Rick Johnson wrote:

> No, classes DO NOT exist at runtime OR compile time! Classes are only
> *structured text* (or code if you prefer) that instruct Python to build
> *real* MEMORY OBJECTS for us. The "magic" that you are witnessing is
> Python, not classes.

Ultimately, everything in Python is "structured text", because the only 
way to create a Python program is to write source code. Everything you 
say is equally true for every other data type in Python. Floats. Strings. 
Ints. Lists. Tuples. Dicts. *Everything*. They are only "structured text" 
when they appear in source code, or on the command line, or in the 
interactive interpreter, just like classes, and Python then constructs an 
object in memory to represent that data structure. Just like classes.

So if you wish to deny that classes are objects, you also have to deny 
that lists and strings and ints and floats are objects too.

In Python, either nothing is an object, or everything is an object. There 
is no middle ground. You cannot treat classes differently from lists, 
because Python treats them the same:

source code of a list literal => list object in memory

source code of a float literal => float object in memory

source code of a class definition => class object in memory

>> py> class Spam(object):
>> ...     pass
>> ...
>> py> id(Spam)
>> 168149924
>> py> isinstance(Spam, type)
>> True
> Do you understand that your object definition named "Spam" is
> transformed into a memory object by python and that the id() function
> and the isinstance() function are operating on a memory object and not
> your structured text?

You don't need a class statement ("object definition") to create a class 
object. Because classes are instances of the metaclass, there is a 
default metaclass (called "type") that does the work of instantiating the 

py> name = "Spam"
py> bases = (object,)
py> dict_ = {}
py> thingy = type(name, bases, dict_)
py> isinstance(thingy, type)
py> thingy
<class '__main__.Spam'>

Classes are instances of type. That is reality in Python.

Classes are objects just like ints and strings and lists. This is a 
fundamental design choice of Python. Deal with it.


More information about the Python-list mailing list