>    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

This also doesn't work for me if a variable is assigned inside the function
before parameters() is called. Although I know you weren't suggesting this
was a final working version, it does at least illustrate that this isn't a
trivial problem to solve.

Whilst this is slightly different from my idea of having a function to set
the class attributes based on the parameters to init, having something like
parameters() would reduce this to a one-liner, and I'd definitely find it
useful for passing to super, which has been mentioned already.

Lewis


On Tue, 5 May 2020, 16:18 Barry Scott, <ba...@barrys-emacs.org> wrote:

>
>
> > 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/
>
_______________________________________________
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/J6BMZISIMHTUFD47S7XFCTEXLALF7AOM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to