>
> 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).
>

I can lay out all the issues with this if you want me to, but after my
previous email I don't think I have to. I'm just wondering why you say it
doesn't need to inspect the underlying source code. That's what
`code_context` is and that's obviously the only place where a string like
`'M('` could be found, unless you want to uncompile bytecode (which is not
impossible either, but it's an additional mess).
_______________________________________________
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/4WQWZCC22L3E42L4SAJP3IYOMHEI3RHE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to