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
A type - every object has exactly one type. For instance, the object
2has a type
intand the object
"joe"has a type
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 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 >>> type(two) <type 'int'> >>> type(type(two)) <type 'type'> >>> type(two).__bases__ (<type 'object'>,) >>> dir(two) ['__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
The type of this
Hmm.. the type
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."
int is an object. This doesn't
mean that just the numbers such as
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
__class__ attribute saying "that guy really
knows me". Calling
type() on an object just returns
the value of the
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.