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

M.-A. Lemburg mal at egenix.com
Mon Jun 6 18:53:23 EDT 2016


On 07.06.2016 00:32, Barry Warsaw wrote:
> On Jun 01, 2016, at 03:53 PM, M.-A. Lemburg wrote:
> 
>> This could be done via a decorator:
>>
>>    @recordbinding
>>    x = obj
>>
>> to result in the compiler generating the following code:
>>
>>    x = obj
>>    obj.recordbinding('x', 2)
> 
> I like the idea of using a decorator because it's familiar syntax and we all
> already (think we) know what it means.  However, in this case, wouldn't
> 
>     @recordbinding
>     x = obj
> 
> translate to
> 
>     x = obj
>     recordbinding(x)
> 
> ?
> 
> The way you've written it, obj must be a type that implements the
> recordbinding method, but that's not what I'd expect.

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.

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.

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

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Jun 07 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list