[Python-ideas] Inline Functions - idea

Ethan Furman ethan at stoneleaf.us
Wed Feb 5 21:59:53 CET 2014


On 02/05/2014 12:45 PM, Alex Rodrigues wrote:
>
> I like the idea of requiring that inline functions be called
>  in a special way that denotes them.

I don't.  ;)

> Otherwise it does add a much bigger area that a maintainer must
>  look for possible bugs. Also on the topic of locals(), note this
> from the python  docs:
>
>> Note
>>
>> The contents of this dictionary [the locals() dict] should not
>>  be modified; changes may not affect the values of local and free
>> variables used by the interpreter.

Irrelevant.  `**locals()` expands the locals dict to be keywords and values, which are then gathered into a new dict:

--> def print_me(**args):
...   print(id(args))
...   for k, v in args.items():
...     print(k, v)

--> def tester():
...   a = 1
...   b = 2
...   c = 3
...   d = locals()
...   print(id(d))
...   print_me(**d)

--> tester()
33842528  # locals() dict
33842240  # args dict
('a', 1)
('c', 3)
('b', 2)

As you can see, the two dicts are different.  Further, you could have print_me be more specific about what it uses:

--> def print_me(a, b, **not_used):
...   print(a, b)

and still call it with **locals().

--
~Ethan~

P.S.  Tested using Python 2.7.


More information about the Python-ideas mailing list