[Python-ideas] combining two threads: switch statements and inline functions

Ron Adam ron3200 at gmail.com
Fri Feb 14 16:33:03 CET 2014



On 02/13/2014 07:12 PM, Chris Angelico wrote:
> On Fri, Feb 14, 2014 at 12:00 PM, Ron Adam<ron3200 at gmail.com>  wrote:
>> >It should be possibly to define a limited type of function that has a code
>> >object which is insert-able into in the byte code, but it would have some
>> >pretty limited restrictions.
>> >
>> >    *  Have all variables declared as non-local.
> By "non-local" do you mean specifically the Python 3 "nonlocal"
> keyword, or is it acceptable to declare globals?

Yes, the Python 3 "nonlocal".  but I took a look at the bytecode, and I 
think it's not worth doing because you need to define the function in a 
function with names that match the function it will be used in.

The globals case is easier...


 >>> from dis import dis
 >>> def foo():
...     global x, y
...     return x + y
...
 >>> dis(foo)
   3           0 LOAD_GLOBAL              0 (x)
               3 LOAD_GLOBAL              1 (y)
               6 BINARY_ADD
               7 RETURN_VALUE

 >>> def bar(x, y):
...     return foo()
...
 >>> dis(bar)
   2           0 LOAD_GLOBAL              0 (foo)
               3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
               6 RETURN_VALUE

In addition to replacing the fist 5 bytecodes here.  There may be some 
index'es that need to be corrected in the surrounding code.


Cheers,
    Ron




More information about the Python-ideas mailing list