[Python-Dev] Definining properties - a use case for classdecorators? (original) (raw)

Guido van Rossum guido at python.org
Wed Oct 19 22:53:04 CEST 2005


On 10/19/05, Fredrik Lundh <fredrik at pythonware.com> wrote:

letting "class" inject a slightly magic "self" variable into the class namespace ?

class C: foo = property(self.getFoo, self.setFoo, None, 'the foo property') def getFoo(self): return self.foo def setFoo(self, foo): self.foo = foo (figuring out exactly what "self" should be is left as an exercise etc)

It's magical enough to deserve to be called self. But even so:

I've seen proposals like this a few times in other contexts. I may even have endorsed the idea at one time. The goal is always the same: forcing delayed evaluation of a getattr operation without using either a string literal or a lambda. But I find it quite a bit too magical, for all values of xyzzy, that xyzzy.foo would return a function of one argument that, when called with an argument x, returns x.foo. Even if it's easy enough to write the solution (*), that sentence describing it gives me goosebumps. And the logical consequence, xyzzy.foo(x), which is an obfuscated way to write x.foo, makes me nervous.

(*) Here's the solution:

class XYZZY(object): def getattr(self, name): return lambda arg: getattr(arg, name) xyzzy = XYZZY()

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list