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

Raymond Hettinger raymond.hettinger at gmail.com
Tue Mar 8 04:50:41 CET 2011


On Mar 7, 2011, at 4:56 PM, Larry Hastings 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)
> . . .
>> I'm not confident any of this is a good idea; luckily this isn't the python-good-ideas-only list. Phew!

Just for the fun of it, here's my offering:
 a := gizmo(arg1, arg2)
is equivalent to
 a = gimzo(arg1, arg2, __name__='a')
Advantages:
 * Doesn't rewrite the order of arguments
 * Keep the current '@' notation unambiguous
 * Still looks like an assignment.
 * Would give a meaningful error message if gizmo() weren't expecting a name
 * Doesn't look like perl
 * Doesn't twist your mind into a pretzel
 * No new keywords
 * Easy to adapt any existing tools that need to know their own name
 * Doesn't seem like magic or spooky action at a distance
 * The program still looks like a Python program
 
Disadvantage:
 * I'm still not sure that the "problem" is worth solving.
 * Between this and function annotations, it looks like Pascal is coming back.
Raymond


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /