[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)

Larry Hastings larry at hastings.org
Tue Mar 8 01:56:25 CET 2011


On 03/03/2011 03:45 PM, Greg Ewing wrote:
> I think we should have assignment decorators.
>
> @decorator
> lhs = rhs
>
> would be equivalent to
>
> lhs = decorator('lhs', rhs)
>

I timidly propose an alternate syntax.  What I don't like about the 
above proposal: assignment is no longer a one-liner.  So let's try it 
inline.

Example 1:

    lhs = @decorator

is equivalent to

    lhs = decorator(classobject, 'lhs', None)


Example 2:

    lhs = @dec1 @dec2

is equivalent to

    lhs = dec2(classobject, 'lhs', dec1(classobject, 'lhs', None))


Example 3:

    lhs = @dec1('string', 3.14)

is equivalent to

    lhs = dec1('string', 3.14)(classobject, 'lhs', None)

(Here you are presumed to return a callable closure with the default 
values baked in.)

Outside class scope, classobject is None.  I think you want the 
classobject there so you can cache information in it, like using the 
variable declarations to build up a per-class database schema.


I'm not confident any of this is a good idea; luckily this isn't the 
python-good-ideas-only list.  Phew!


/larry/



More information about the Python-ideas mailing list