On Sun, Apr 19, 2020 at 2:37 PM Eric V. Smith <e...@trueblade.com> wrote:

> So, if M() existed, you could say:
>
> d = M(telephone, name)
> func(**d)
>
> or
>
> func(**M(telephone, name))
>
Per your wish, Eric, the glorious successor of Q() ... named M():

>>> def M(*vals):
...     import sys
...     import inspect
...     caller = sys._getframe(1)
...     call = inspect.stack()[1].code_context[0]
...     _, call = call.split('M(')
...     call = call.strip()[:-1]
...     names = [name.strip() for name in call.split(',')]
...     dct = {}
...     for name in names:
...         dct[name] = eval(name, globals(), caller.f_locals)
...     return dct
...
>>> x, y, z = range(3)
>>> M(x, y, z)
{'x': 0, 'y': 1, 'z': 2}

OK, it's a little bit fragile in assuming the function must be called M
rather than trying to derive its name.  And maybe my string version of
finding the several args could be made more robust.  But anyone is welcome
to improve it, and the proof of concept shows that's all we need.
Basically, a "dict-builder from local names" is perfectly amenable to
writing as a Python function... and we don't need to inspect the underlying
source code the way I believe Alex' sorcery module does (other parts of it
might need that, but not this).

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/L4D47KAFLBYKW3EDTD5FAUPTHPJP6GNC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to