[Python-ideas] wild idea : target decorators
Guido van Rossum
guido at python.org
Fri Sep 25 16:41:55 CEST 2009
On Fri, Sep 25, 2009 at 2:57 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>> So, you don't care abut the repetition of x in "x = f(x)" but you'd
>> like to have a way to implicitly pass an argument giving the string
>> name of the target variable, like "x = f('x')" ?
> Yes. I'm defining a property, and the descriptor happens
> to need to know the name of the property being defined,
> but that's an implementation detail. The user shouldn't
> be required, or even allowed, to specify it as an
> independent parameter.
>> I'm not sure that it's common enough to warrant special
> Probably not very common in general, but I find myself
> doing this very intensively in some of my projects. I
> have two GUI libraries in which nearly every externally
> visible attribute is one of these properties.
A common pattern for this is to have a metaclass that walks over all
the properties in the class (which are easily recognized using
isinstance() or some other check) and calls them with an
initialization function that passed in their name.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-ideas