ADVANCED PYTHON PROGRAMMING

Objects—Objects Everywhere

This time, we cover objects—which includes everything, so we focus on the abstraction—IDs, types and values—and some basic behaviors.

Dan Gittik
12 min readApr 20, 2020

--

In Python, everything is an object. Well, almost everything—keywords like if are not; but everything else is: numbers, strings, functions, instances—even classes! Each object has a unique ID, a type that defines its behavior, and a value that parametrizes it—but contrary to popular belief, the most important (and interesting) part of this trinity is the type. In this article, we’ll understand why, and see some of the fascinating behaviors we can imprint unto custom objects.

But first thing first: let’s talk about object-oriented programming, or OOP. So far, we’ve dealt with procedural programming: in this paradigm, abstractions are encapsulated as procedures, or functions, which operate on data. The biggest challenge here, then, is representing state; you have to use elaborate data structures, and pass them along into a myriad of functions, which construct new data structures or mutate existing ones.

In object-oriented programming, the paradigm is such that abstractions are encapsulated in objects: a combination of data, accessible through attributes, and code, represented by methods. These methods automatically get a reference to their instance—traditionally called self—so that they can manage that instance’s state. In many cases, this is a more intuitive way to represent the world—and our way to wield it is through custom classes, which describe our object’s structure and behavior.

Everything is an Object

There’s a saying in creative writing: “show, don’t tell”. Well:

As you can see, everything—from numbers to classes—can be represented as a string, even if a useless one like <A object at 0x...>; and everything has a class, accessible through the __class__ attribute, which points to its creator.

Seeing Some ID

Like I said, the first thing to address is the ID:

When an object is created, it’s given a unique identifier—in CPython, this is actually the memory address where its PyObject is allocated. That ID can be retrieved with the built-in id function, and compared against using the is keyword.

At first glance, is looks pretty stupid; isn’t it pretty much the same as equality? And if not, would an object really be anything but itself—so what’s the point in having a separate operator to test for it?

As it turns out, identity is different to equality: two objects representing the same file, or user, may be considered the same—but in actuality, be two separate entities, somewhere in memory. In other words, == compares two objects’ values, whereas is compares their identity. This is yet another way to test our previous discovery, that everything in Python is passed by reference:

As you can see, even though a1 and a2 represent pretty much the same (empty) object of type A, only a1—and every other name bound to the exact same object—is identical to itself. With objects that change, often called mutable objects, the benefit is clear: we have a way to identify a particular object, no matter its current value. With objects that don’t change, called immutable objects, it’s less obvious; so use == instead, unless you really know what you’re doing. For example:

Since integers are immutable, Python figures it might as well cache them; so when you refer to 1, which denotes the integer object whose value is 1, you always get the same instance—resulting in identity even with independent assignment statements. However:

Turns out, Python only caches commonly used integers—like the range from 0 to 127 or something. For big numbers, it allocates a new integer object with its unusual value every time, resulting in different objects that don’t test identical.

If this seems confusing, it’s because engaging in such exploratory research often reveals nuances and edge-cases that can be a bit overwhelming; so it’s important to step back and have another look at the bigger picture before moving forward. The bottom line is, every object in Python is assigned a unique identifier, accessible through id and testable through is, which lets us distinguish between objects even when their value is the same; moving on.

The Holy Trinity

Then we have the type. We saw that it’s accessible through the __class__ attribute, but a more elegant way to get it would be with the built-in type function:

This is just a pointer to some other object; and understanding the nature of this relation is what we’ll spend most of this chapter talking about.

But before we do—there’s also the value: some configuration, or state, which parametrizes the object’s behavior, much like arguments parametrize the execution of a function. Take 1, for example:

Its value is such, that when Python asks it for its representation, it returns the ASCII character 0x31, or 1. This is in fact encoded in its class’s __repr__ method:

Similarly, we can add two integers up, and their values determine the value of their sum:

Which is actually encoded in the __add__ method:

It turns out, integers even have methods of their own, such as to_bytes, which serializes it into some number of bytes with some endianness:

This is actually pretty complex behavior: first, we have to resolve the integer’s attribute, which is handled by __getattribute__:

And then, we have to invoke that object, which is handled by __call__:

So as you can see, while formally the holy trinity comprises of the ID, the type and the value—the really interesting bit is the type, which defines the behavior; the ID being more of a technicality, and the value being merely the argument to that behaviour’s function.

Mutability

One thing the type determines is mutability—whether an object can change or not. Some objects can’t, no matter how hard you want it:

While others can:

Unfortunately, even a concept as simple as this has its pitfalls; for example, if immutable objects can’t change, and tuples are immutable, then how come I can do this:

Booyah—the representation changed, so the value must be different. Except it’s not: the tuple’s value has only ever been three pointers, referencing an empty list and two integers; its representation behavior just so happened to be recursive, and delegate to those references. When we accessed the list and changed it, the tuple didn’t change: it’s still pointing to the same objects, and it always will. This can be further exemplified by this funny bug:

At first sight, we create a list of 5 empty lists; in actuality, we create a list with one empty list, and then multiply it by 5, resulting of a list with five of those—that is, five references to the same empty list, which grow and shrink together, to everyone’s surprise and delight.

But anyway, the question of mutability just scratches the surface: there’s a whole lot more behaviors one can customize, if one was so inclined. And seeing as we are, let start this exciting journey—although truth be told, much like in the Lord of the Rings, our epic quest might seem a bit tedious at times. So grab a cookie, your favorite hot beverage, and a Mithril shirt:

Behavior Modification

Generally speaking, when you define a class, you do something like this:

And then when you have an instance, a = A(), it behaves in this way under the appropriate circumstances. The simplest example of that is asking an object to display itself in some human-readable way:

This is effectively telling instances of A how to behave when being cast to string, which is what the print function secretly does to all its arguments. There’s another, more developer-oriented representation, which is what you get in when you just “dump” the object in the interpreter:

This behavior is controlled by __repr__, and intended to provide more technical information, so when the object is inspected in a debugging context, it’s easier to understand. If you can, it’s recommended to return the constructor invocation which yielded the object—or, if it isn’t very readable, or has a very volatile state, some other description between angle brackets:

Notice the !r at the end of the name formatting? It’s a way to invoke the formatee’s __repr__ behavior (instead of the standard __str__), which is especially handy in recursive representations such as this. Without it, we’d get the syntactically incorrect User(Alice)—with it, we get the quotes, and Python is even smart enough to figure out how to escape trickier strings like "O'Brian".

But as we venture into the much more complex world of classes, already we start running into interesting caveats. Imagine I’d subclass User with a more specific Admin:

That’s rather unfortunate—it seems like we’d have to override the __repr__ method for every subclass, even if they’re virtually identical. Or—

We can prevent the problem altogether by writing the original __repr__ in such a way, that the class’s name is resolved dynamically. You get this:

Pretty neat, no? But not at all obvious. Just wait until we talk about equality…

Equality

Let’s talk about equality. Imagine we have a class with a single attribute of a simple integer, which represents that object’s entire value and state. We’d want objects with a similar integer to test equal, right? Alas—

That happens because we haven’t defined any custom comparison behavior, and Python defaults to comparing objects by their ID, which is always unique—so an object is only ever equal to itself. Unless…

That’s pretty nifty—but breaks rather quickly:

What happened is that 1 was passed into __eq__, which tried to access its x attribute—but since it doesn’t have one, it caused an exception. We can argue whether, on some philosophical level, an object that’s wholly represented by some number should be equal to the number itself—but for our purposes, let’s decide A objects can only be equal to other A objects:

However, this is easier said than done: what if we have a subclass again?

This happens because we’ve hardcoded class A in the equality operator, and neither b1 nor b2 have a type of A. What we should’ve used is the more nuanced built-in function, isinstance:

What happens is, isinstance is smart enough to traverse the entire class hierarchy, and it figures that indeed, the bs are instances (albeit, indirect descendants) of A—so it carries on with the rest of the test, which evaluates to True.

Inequality

To achieve the opposite, simply implement __ne__, for not equal. Python is actually smart enough to fall back to not __eq__, so if your logic is reversed (as it really should be), there’s no need to write it yourself. The case is far more interesting with the other comparison operators:

Alas, a new caveat arises. What if we define a class that is always less than anything else; let’s call it Epsilon—

In that case, this works:

Because it calls e.__lt__(a1), which always returns True; but this doesn’t:

Because it calls a1.__gt__(e), which notices e is not an instance of A and returns False. While it’s OK for __eq__ to return False when the object type is different, because it clearly implies the object is not equal to self—this isn’t the case with the other comparison operators, which require a third option: NotImplemented; that is to say—I don’t know. The reason this is so much better than a definitive “no”, is that it gives Python a chance to go and ask the other party involved—maybe its reverse operator would shed some light on the problem. And if neither has any idea, Python will raise a standard TypeError on their behalf, which is much more informative than a decisive refusal. Here’s the proper implementation:

In this case…

…works! a1.__gt__(e) is still invoked first, but since it returns NotImplemented, Python goes ahead and tries e.__lt__(a1), which is kind enough to return True. This technique is relevant to all the comparison operators:

One last word of caution—for some reason, the Python developers decided not to extrapolate whatever comparison function you define into what’s called a “total order”. The opposite of > is ≤, right? Well:

You can still achieve the desired effect rather easily, by using the total_ordering decorator in the standard functools module. All you need to do is define __eq__ and one other comparison operator, and it’ll infer the rest using the powers of math:

If you’re weirded out by seeing a decorator on top of a class, that’s alright; we’ll get there shortly. Maybe not that shortly, seeing as we’ve only covered display, equality and comparisons—but we’ll get there.

Conclusion

Objects are everywhere—and in Python, I mean it quite literally. Understanding them is really mostly about understanding their type, which defines their behaviors, and is encoded in a class. We saw how “magic methods”, starting and ending with a double-underscore (or “duner”, for short) let us customize how our objects behave in certain circumstances—and we’ll learn many more as we push forward. Next time we’ll cover exotic behaviors such as arithmetics, invocation, indexing and iteration, and then on to the holy grail — attributes and method resolution.

The Advanced Python Programming series includes the following articles:

  1. A Value by Any Other Name
  2. To Be, or Not to Be
  3. Loopin’ Around
  4. Functions at Last
  5. To Functions, and Beyond!
  6. Function Internals 1
  7. Function Internals 2
  8. Next Generation
  9. Objects — Objects Everywhere
  10. Objects Incarnate
  11. Meddling with Primal Forces
  12. Descriptors Aplenty
  13. Death and Taxes
  14. Metaphysics
  15. The Ones that Got Away
  16. International Trade

--

--

Dan Gittik

Lecturer at Tel Aviv university. Having worked in Military Intelligence, Google and Magic Leap, I’m passionate about the intersection of theory and practice.