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

M.-A. Lemburg mal at egenix.com
Wed Jun 1 09:53:04 EDT 2016


On 01.06.2016 15:19, Sven R. Kunze wrote:
> On 01.06.2016 01:46, Ian Foote wrote:
>> This is the first syntax I've seen in this thread that seems reasonably
>> intuitive and pythonic. I don't mind particularly the keyword used (def
>> or something else), but this structure draws the right parallels in my
>> mind.
> 
> Still nobody explained why it needs a special syntax at all.
> 
> Normal assignment could do it as well as long as the assignment tells
> the RHS under what name it will be referred to.

Right. Essentially, we'd only need a way to tell the compiler
to invoke e.g. a method on the RHS object which gets called
with the name of the variable it binds (and perhaps the line
number to allow for e.g. recording the order of definitions).

The current byte code for:

    x = obj

reads like this:

  2           0 LOAD_GLOBAL              0 (obj)
              3 STORE_FAST               0 (x)

so if we could get the compiler to generate a method call
right after the assignment, e.g. obj.recordbinding('x', 2)
(taking as arguments the name of the variable and the line
number) we could do lots of interesting stuff.

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)

Alternativey, you could do all this without any changes
to the interpeter by using a trace function which traces
the execution of a block:

    start_recordbingings() # enable trace function
    x = obj # trace function detects assignment and calls
            # obj.recordbinding('x', 2)
    stop_recordbindings() # disable trace function

However, this is very slow.

A second option would be to place the above into a function
definition and have a decorator apply the byte code
manipulations to implement the implicit call:

@recordbindings
def code():
    x = obj

but this would create problems with the local/global
namespaces.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Jun 01 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