Chapter 1. Basic Concepts

The Object Within

So what exactly is a Python object? An object is an axiom in our system - it is the notion of some entity. We still define an object by saying it has:

  • Identity (i.e. given two names we can say for sure if they refer to one and the same object, or not).

  • A value - which may include a bunch of attributes (i.e. we can reach other objects through objectname.attributename).

  • A type - every object has exactly one type. For instance, the object 2 has a type int and the object "joe" has a type string.

  • One or more bases. Not all objects have bases but some special ones do. A base is similar to a super-class or base-class in object-oriented lingo.

If you are more of the 'I like to know how the bits are laid out' type as opposed to the 'I like the meta abstract ideas' type, it might be useful for you to know that each object also has a specific location in main memory that you can find by calling the id() function.

The type and bases (if they exist) are important because they define special relationships an object has with other objects. Keep in mind that the types and bases of objects just other objects. This will be re-visited soon.

You might think an object has a name but the name is not really part of the object. The name exists outside of the object in a namespace (e.g. a function local variable) or as an attribute of another object.

Even a simple object such as the number 2 has a lot more to it than meets the eye.

Example 1.1. Examining an integer object

>>> two = 2 1
>>> type(two)
<type 'int'> 2
>>> type(type(two))
<type 'type'> 3
>>> type(two).__bases__
(<type 'object'>,) 4
>>> dir(two) 5
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', 
 '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
 '__floordiv__', '__format__', '__getattribute__', '__getnewargs__',
 '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__',
 '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__',
 '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
 '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__',
 '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
 '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
 '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__',
 '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__',
 'conjugate', 'denominator', 'imag', 'numerator', 'real']


Here we give an integer the name two in the current namespace.


The type of this object is <type 'int'>. This <type 'int'> is another object, which we will now explore. Note that this object is also called just int and <type 'int'> is the printable representation.


Hmm.. the type of <type 'int'> is an object called <type 'type'>.


Also, the __bases__ attribute of <type 'int'> is a tuple containing an object called <type 'object'>. Bet you didn't think of checking the __bases__ attribute ;).


Let's list all the attributes present on this original integer object - wow that's a lot.

You might say "What does all this mean?" and I might say "Patience! First, let's go over the first rule."

Rule 1

Everything is an object

The built-in int is an object. This doesn't mean that just the numbers such as 2 and 77 are objects (which they are) but also that there is another object called int that is sitting in memory right beside the actual integers. In fact all integer objects are pointing to int using their __class__ attribute saying "that guy really knows me". Calling type() on an object just returns the value of the __class__ attribute.

Any classes that we define are objects, and of course, instances of those classes are objects as well. Even the functions and methods we define are objects. Yet, as we will see, all objects are not equal.