Date
Tags python

Intro

This post describes a fun little piece of code I wrote a few years back. It's presented here just as a curiosity not as a good idea for production code, and more than anything else is just an experiment to demonstrate the flexibility of python as a language. So let's play with function-call semantics. This writeup is just some background and motivation, if you're more of a "show me the code!" kind of person, just check out the gist here.

The Idea

We use "self" all the time in object-oriented python code as an object reference, similar to "this" in Java or Javascript. It's important to note though that in Python, "self" is only a convention and not a keyword. When we use "self", the reference is always unambiguous and simple.. we just mean the current instance. But they say that "art imitates life", so what if object-oriented was inspired by how people work?

Persons always have one self but potentially many roles. For instance, suppose someone asks you a question. It's possible you will answer differently depending on whether you are answering in your capacity as that person's colleague / boss / friend / lover / enemy / etc. Anyway, the code below contains a (contrived) example. 1.

Another way of looking at the concept being demonstrated: here there is not one self but many, and what a thing does depends on what you call it, not how you call it.

The Code

  1. See also: http://www.metrolyrics.com/the-philosophers-song-lyrics-monty-python.html