-
Tip of the day: Virtual object properties
-
Here’s a trick I learned today from Chris that’s worth sharing: creating “virtual” attributes for objects (in need for a better wording).
Here’s a use-case: we have a person with a Name and a Surname — how could we access its full name in one string like
person.full_name? You could do that with Python’s@propertydecorator:class Person(object): def __init__(self, name, surname): self.name = name self.surname = surname @property def full_name(self): return "%s %s" % (self.name, self.surname) p=Person('John', 'Doe') print (p.name, p.surname) ('John', 'Doe') print p.full_name John Doe
Without the decorator we would need to access the string as
person.full_name(), instead of the simpler and more intuitiveperson.full_name. Pretty neat.

It’s amusing to see how Python and the other “modern” languages are moving closer to Lisp as time passes.
What you describe with the ‘decorators’ is something that has been available as a very very useful side-effect of ‘generic functions” in Lisp.
For example, here’s how one would define a person class with (name, surname) as its only attributes in Lisp:
Now the functions (name person-object) and (surname person-object) work fine, because of the ‘accessor’ methods defined in the class, so you can write:
This creates an instance of the person class, and calls the two accessor methods to display the existing attributes.
To build an arbitrary combination of the attributes, you can either concatenate the results right there, or define a new generic function which abstracts away the implementation details of the method.
Note that the person class is defined by now, but I can type directly in the same Lisp session the following:
This defines a new generic function that can work on person objects, but doesn’t really implement anything yet. You can think of this as a virtual class method in other OO languages.
Now, let’s implement the actual function:
That’s it. We now have a new method that knows how to access the appropriate attributes of a person object, do whatever it takes to construct the full name of the person, and return it as a string:
The beauty and power of this is that the special method for doing all this was defined after the original class was set in stone. No changes are needed to the original class, because the model of the generic functions in Lisp allows us to extend the functionality of a class in runtime, long after the original class was developed.
Now, that’s what I call cool :-)
Interesting :-)
Funny thing, I’ve been using properties a lot this week. But I’ve had to use the non-decorator syntax since I’m using them for setters as well as getters. Basically, I wanted to keep a variable settable by the user loosely synced with a copy saved in the filesystem or in a web application. To do this, I had a getter to get the value from the cache and a setter to put it in whenever the element was referenced. Minus error handling, it boiled down to this:
python 2.6+ should be able to handle this with a decorator as well.
Thanks for the information :)