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

Chris Kaynor ckaynor at zindagigames.com
Tue Mar 8 02:08:46 CET 2011


On Mon, Mar 7, 2011 at 4:56 PM, Larry Hastings <larry at hastings.org> wrote:

>
> 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)
>

You seemed to have missed the example of:

lhs = @dec1 123

which would produce

dec1(classobject, 'lhs', 123)

correct?

If so, the separation between dec1 and 123 seems too little - it looks like
typos like:
lhs = @dec1123
lhs = @dec123

would be easy to make and difficult to detect.


>
> (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/
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110307/3fa6603c/attachment.html>


More information about the Python-ideas mailing list