[Python-ideas] Re: Passing Arguments Through Thin Wrappers (was : Auto-assign attributes from __init__ arguments)

2020-05-05 Thread Lewis Ball
>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,  wrote:

>
>
> > On 5 May 2020, at 15:38, Steven D'Aprano  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 
> 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/


[Python-ideas] Re: Passing Arguments Through Thin Wrappers (was : Auto-assign attributes from __init__ arguments)

2020-05-05 Thread Barry Scott



> On 5 May 2020, at 15:38, Steven D'Aprano  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 
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] Re: Passing Arguments Through Thin Wrappers (was : Auto-assign attributes from __init__ arguments)

2020-05-05 Thread Dominik Vilsmeier

On 05.05.20 15:35, Dan Sommers wrote:


On Tue, 5 May 2020 23:06:39 +1000
Steven D'Aprano  wrote:


... help me solve the DRY problem for module-level functions:

 def function(spam, eggs, cheese, aardvark):
 do stuff
 call _private_function(spam, eggs, cheese, aardvark)

since this bites me about twice as often as the `self.spam = spam`
issue.

(That's not me being snarky by the way, it's a genuine question:
dataclasses are a mystery to me, so I don't know what they can and can't
do.)

Lisp macros have a "" feature that captures the entire collection
of arguments to the macro:

 http://clhs.lisp.se/Body/03_dd.htm

Perhaps Python could adopt something similar?  Unlike *args and
**kwargs,  captures all of the parameters, not just the
non-positional, non-named ones.  The idea would be something like this:

 def function(spam, eggs, cheese, aardvark, ):
 do_stuff
 _private_function()

which would call _private_function as function was called.


What about a way for overloading function signatures? The arguments are
then bound to both signatures and the function has access to all the
parameters. For example:

    def function(spam, eggs, cheese, aardvark) with (*args):
    ...  # do stuff
    _private_function(*args)

Calling `function(1, 2, 3, 4)` results in `spam, eggs, cheese, aardvark
= 1, 2, 3, 4` and `args = (1, 2, 3, 4)`.
___
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/OHGIK5YHET6YUANFDT7RRXRT47AIAYM5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Passing Arguments Through Thin Wrappers (was : Auto-assign attributes from __init__ arguments)

2020-05-05 Thread Steven D'Aprano
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


-- 
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/