[Python-ideas] Quick idea: defining variables from functions that take the variable name

Barry Warsaw barry at python.org
Mon Jun 6 19:14:49 EDT 2016


On Jun 07, 2016, at 12:53 AM, M.-A. Lemburg wrote:

>This would work as well and indeed reads better, but you'd need
>to have the compiler generate:
>
>      x = obj
>      recordbinding(obj, 'x', 2)
>
>ie. pass in the object, the bound name and the line number
>and recordbinding would then have to decide what to do with the
>parameters.

+1 although I'd bikeshed on the order of the arguments.

>I used the method variant, because a very common use case
>is to let the object know about the name under which it is
>now known to Python. This can be used to eg. define records,
>forms, mappings, etc.

Yep.  So given the above, `recordbinding(obj, 'x', 2)` would of course be free
to delegate to `obj.recordbinding('x', 2)`, but it would be up to the
decorator (and its author), not the compiler to do that delegation.

>I just wonder how we could tell the compiler to special
>case this decorator in a clean way.

That's the rub, but I do think you're on to a good general solution to a
common set of problems.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160606/ea105785/attachment.sig>


More information about the Python-ideas mailing list