Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101255 > unrolled thread
| Started by | Robert <rxjwg98@gmail.com> |
|---|---|
| First post | 2016-01-04 18:16 -0800 |
| Last post | 2016-01-05 12:51 +0100 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
Is '*args' useful in this example code? Robert <rxjwg98@gmail.com> - 2016-01-04 18:16 -0800
Re: Is '*args' useful in this example code? Ben Finney <ben+python@benfinney.id.au> - 2016-01-05 13:26 +1100
Re: Is '*args' useful in this example code? Robert <rxjwg98@gmail.com> - 2016-01-04 18:34 -0800
Re: Is '*args' useful in this example code? Ben Finney <ben+python@benfinney.id.au> - 2016-01-05 14:28 +1100
Re: Is '*args' useful in this example code? Robert <rxjwg98@gmail.com> - 2016-01-05 05:04 -0800
Re: Is '*args' useful in this example code? Arif Khokar <akhokar1234@wvu.edu> - 2016-01-04 22:29 -0500
Re: Is '*args' useful in this example code? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-01-05 12:51 +0100
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-04 18:16 -0800 |
| Subject | Is '*args' useful in this example code? |
| Message-ID | <6b0e236f-b240-486d-95e6-17bba9458cde@googlegroups.com> |
Hi,
I find an example code on wrap at this link:
http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
Here is the code:
////////
def logged(func):
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
///////
I understand now, but I feel the args usage is weird. I don't see any way
to use *args and **kwargs in above code. What is your opinion on it?
Thanks,
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-01-05 13:26 +1100 |
| Message-ID | <mailman.10.1451960795.2305.python-list@python.org> |
| In reply to | #101255 |
Robert <rxjwg98@gmail.com> writes: > I understand now, but I feel the args usage is weird. I don't see any way > to use *args and **kwargs in above code. What is your opinion on it? Can you show example code that you would expect, and specifically what about the actual code doesn't match what you expect? -- \ “Of course, everybody says they're for peace. Hitler was for | `\ peace. Everybody is for peace. The question is: what kind of | _o__) peace?” —Noam Chomsky, 1984-05-14 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-04 18:34 -0800 |
| Message-ID | <ca45fc5d-60ce-4df2-a441-0e85a4b05003@googlegroups.com> |
| In reply to | #101257 |
On Monday, January 4, 2016 at 9:26:47 PM UTC-5, Ben Finney wrote:
> Robert <rxjwg98@gmail.com> writes:
>
> > I understand now, but I feel the args usage is weird. I don't see any way
> > to use *args and **kwargs in above code. What is your opinion on it?
>
> Can you show example code that you would expect, and specifically what about
> the actual code doesn't match what you expect?
>
> --
> \ "Of course, everybody says they're for peace. Hitler was for |
> `\ peace. Everybody is for peace. The question is: what kind of |
> _o__) peace?" --Noam Chomsky, 1984-05-14 |
> Ben Finney
Excuse me for the incomplete info previously.
If I call it with
a = f(3)
the result is 12. It is correct as below message. That is no use of *args
and **kwargs.
If I put more parameters in f, it will give errors as below.
///////////
%run "C:/Users/pyprj/ipython0/decor0.py"
f was called
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\pyprj\ipython0\decor0.py in <module>()
11 return x + x * x
12
---> 13 a = f(3, 4)
14
C:\Users\pyprj\ipython0\decor0.py in with_logging(*args, **kwargs)
3 def with_logging(*args, **kwargs):
4 print func.__name__ + " was called"
----> 5 return func(*args, **kwargs)
6 return with_logging
7
TypeError: f() takes exactly 1 argument (2 given)
%run "C:/Users/rj/pyprj/ipython0/decor0.py"
f was called
a
Out[13]: 12
///////////
I don't see *args and **kwargs can be used by other way yet.
Do you think this internal function definition (with *args) is useful?
Thanks,
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-01-05 14:28 +1100 |
| Message-ID | <mailman.11.1451964493.2305.python-list@python.org> |
| In reply to | #101258 |
Robert <rxjwg98@gmail.com> writes: > On Monday, January 4, 2016 at 9:26:47 PM UTC-5, Ben Finney wrote: > > Can you show example code that you would expect, and specifically what about > > the actual code doesn't match what you expect? > > Excuse me for the incomplete info previously. > If I call it with > a = f(3) > the result is 12. Can you show a *very* simple example of the ‘f’ you're talking about? Contrive a new one, make it behave the same way while keeping it very short, and once you have that, put that code in your message. With an actual function to examine it will be much easier to know where the confusion lies. -- \ “When a well-packaged web of lies has been sold to the masses | `\ over generations, the truth will seem utterly preposterous and | _o__) its speaker a raving lunatic.” —Dresden James | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-05 05:04 -0800 |
| Message-ID | <aeb127ae-f1a4-4cc6-965b-693cdd50036e@googlegroups.com> |
| In reply to | #101259 |
On Monday, January 4, 2016 at 10:28:31 PM UTC-5, Ben Finney wrote: > Robert <.com> writes: > > > On Monday, January 4, 2016 at 9:26:47 PM UTC-5, Ben Finney wrote: > > > Can you show example code that you would expect, and specifically what about > > > the actual code doesn't match what you expect? > > > > Excuse me for the incomplete info previously. > > If I call it with > > a = f(3) > > the result is 12. > > Can you show a *very* simple example of the 'f' you're talking about? > Contrive a new one, make it behave the same way while keeping it very > short, and once you have that, put that code in your message. > > With an actual function to examine it will be much easier to know where > the confusion lies. > > -- > \ "When a well-packaged web of lies has been sold to the masses | > `\ over generations, the truth will seem utterly preposterous and | > _o__) its speaker a raving lunatic." --Dresden James | > Ben Finney Excuse me, Ben. I forgot to add the f function. It is as below: @logged def f(x): """does some math""" return x + x * x a = f(3) My problem has been solved after all of your helpful posts. Thanks.
[toc] | [prev] | [next] | [standalone]
| From | Arif Khokar <akhokar1234@wvu.edu> |
|---|---|
| Date | 2016-01-04 22:29 -0500 |
| Message-ID | <lMGiy.73771$8V1.56160@fx28.iad> |
| In reply to | #101255 |
On 01/04/2016 09:16 PM, Robert wrote: > Hi, > > I find an example code on wrap at this link: > http://stackoverflow.com/questions/308999/what-does-functools-wraps-do > > Here is the code: > //////// > def logged(func): > def with_logging(*args, **kwargs): > print func.__name__ + " was called" > return func(*args, **kwargs) > return with_logging > /////// > > I understand now, but I feel the args usage is weird. I don't see any way > to use *args and **kwargs in above code. What is your opinion on it? The reason the inner method has *args, and **kwargs as parameters is so that it doesn't have to exactly match the signature of func. If func was a method that took 3 parameters, then if you didn't use *args, you would have to define the with_logging method to also take 3 parameters. If func takes one or more keyword parameters, then you would have to add that to the definition of the with_logging method if you don't use **kwargs. IOW, using *args and **kwargs in the wrapper method definition removes the requirement that its parameter list exactly match func's parameter list.
[toc] | [prev] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2016-01-05 12:51 +0100 |
| Message-ID | <mailman.15.1451994780.2305.python-list@python.org> |
| In reply to | #101255 |
Op 05-01-16 om 03:16 schreef Robert:
> Hi,
>
> I find an example code on wrap at this link:
> http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
>
> Here is the code:
> ////////
> def logged(func):
> def with_logging(*args, **kwargs):
> print func.__name__ + " was called"
> return func(*args, **kwargs)
> return with_logging
> ///////
>
> I understand now, but I feel the args usage is weird. I don't see any way
> to use *args and **kwargs in above code. What is your opinion on it?
>
>
> Thanks,
You want the following to work
def add(a, b):
return a + b
def double(n)
return n + n
logged_add = logged(add)
logged_double = logged(double)
s = logged_add(3, 5)
d = logged_double(13)
--
Antoon Pardon
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web