> On 5 May 2020, at 15:38, Steven D'Aprano <st...@pearwood.info> wrote:
>
> Here is a quick and dirty proof of concept:
>
>
> from inspect import stack, Signature
>
> def parameters():
> caller = stack()[2][0].f_globals[stack()[1][3]]
> sig = Signature.from_callable(caller)
> vars = stack()[1][0].f_locals
> return sig.bind(**vars).arguments
>
>
> def func(spam, eggs):
> params = parameters()
> for name, value in params.items():
> print(name, '=', value)
>
>
> Calling `func(2, 3)` prints:
>
> spam = 2
> eggs = 3
Is this to avoid locals()?
Do you have a version that works inside a func of a class?
I tried the obvious and it TB'ed:
from inspect import stack, Signature
def parameters():
caller = stack()[2][0].f_globals[stack()[1][3]]
sig = Signature.from_callable(caller)
vars = stack()[1][0].f_locals
return sig.bind(**vars).arguments
class X:
def func(self, spam, eggs):
params = parameters()
for name, value in params.items():
print(name, '=', value)
x = X()
x.func( 1, 2 )
Traceback (most recent call last):
File "args.py", line 25, in <module>
x.func( 1, 2 )
File "args.py", line 20, in func
params = parameters()
File "args.py", line 11, in parameters
caller = stack()[2][0].f_globals[stack()[1][3]]
KeyError: 'func'
Barry
>
>
> --
> Steven
> _______________________________________________
> 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/62ZDUVMEKQEMBRTVHM2GYXZVJLPPHYBQ/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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/7BVSYMV4Q7SOOEGC5UDLMWVZ6XT5RVUY/
Code of Conduct: http://python.org/psf/codeofconduct/