473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

forwarding *arg parameter

>>def g(*arg):
.... return arg
....
>>g('foo', 'bar')
('foo', 'bar')
>># seems reasonable
....
>>g(g('foo', 'bar'))
(('foo', 'bar'),)
>># not so good, what g should return to get rid of the outer tuple
TV
Nov 5 '06 #1
16 1287
Tuomas schrieb:
>>def g(*arg):
... return arg
...
>>g('foo', 'bar')
('foo', 'bar')
>># seems reasonable
...
>>g(g('foo', 'bar'))
(('foo', 'bar'),)
>># not so good, what g should return to get rid of the outer tuple
g(*g('foo', 'bar'))
* and ** are the symetric - they capture ellipsis arguments, and they
make iterables/dicts passed as positional/named arguments.

Diez
Nov 5 '06 #2
Diez B. Roggisch wrote:
Tuomas schrieb:
> >>def g(*arg):
... return arg
...
> >>g('foo', 'bar')
('foo', 'bar')
> >># seems reasonable
...
> >>g(g('foo', 'bar'))
(('foo', 'bar'),)
> >># not so good, what g should return to get rid of the outer tuple


g(*g('foo', 'bar'))
* and ** are the symetric - they capture ellipsis arguments, and they
make iterables/dicts passed as positional/named arguments.

Diez
Thanks Diez

And what about this case if I want the result ('foo', 'bar')
>>def f(*arg):
.... return g(arg)
....
>>f('foo', 'bar')
(('foo', 'bar'),)
>>def h(*arg):
.... return arg[0]
....
>>g=h
f('foo', 'bar')
('foo', 'bar')

Where can g know it should use arg[0] when arg is forwarded?

TV
Nov 5 '06 #3
Tuomas schrieb:
>>def g(*arg):
... return arg
...
>>g('foo', 'bar')
('foo', 'bar')
>># seems reasonable
...
>>g(g('foo', 'bar'))
(('foo', 'bar'),)
>># not so good, what g should return to get rid of the outer tuple

TV
Use the following then:
>>g(*g('foo', 'bar'))
('foo', 'bar')

Otherwise, you would have to check if arg is a 1-tuple consisting of a
tuple and "strip" it out then.

e.g.
>>def g(*arg):
.... return arg[0] if isinstance(arg[0], tuple) else arg
....
>>g('foo', 'bar')
('foo', 'bar')
>>g(g('foo', 'bar'))
('foo', 'bar')
Nov 5 '06 #4
On Sun, 05 Nov 2006 15:26:58 +0000, Tuomas wrote:
>>def g(*arg):
... return arg
...
>>g('foo', 'bar')
('foo', 'bar')
>># seems reasonable
The function g:
- takes the arguments 'foo' and 'bar'
- collects them in a tuple named 'arg' = ('foo', 'bar')
- returns the tuple named arg

>>g(g('foo', 'bar'))
(('foo', 'bar'),)
The function g:
- takes the argument ('foo', 'bar')
- collects it in a tuple named 'arg' = (('foo', 'bar'),)
- returns the tuple named arg

The function is doing exactly the same as in the first case, except the
arguments are different.

>># not so good, what g should return to get rid of the outer tuple
Why do you want to? The way the function is now makes perfect sense. All
argument types are treated in exactly the same way:

g(string) =tuple containing string
g(float) =tuple containing float
g(int) =tuple containing int
g(list) =tuple containing list
g(instance) =tuple containing instance
g(tuple) =tuple containing tuple

You could write something like this:

def g(*arg):
# Detect the special case of a single tuple argument
if len(arg) == 1 and type(arg[0]) == tuple:
return arg[0]
else:
return arg

but now tuple arguments are treated differently to all other data. Why do
you think you need that?
--
Steven

Nov 5 '06 #5
Steven D'Aprano wrote:
<snip>
You could write something like this:

def g(*arg):
# Detect the special case of a single tuple argument
if len(arg) == 1 and type(arg[0]) == tuple:
return arg[0]
else:
return arg

but now tuple arguments are treated differently to all other data. Why do
you think you need that?
I am looking a shorter way to do the above in the case:

def g(*arg):
return arg

def f(*arg):
return g(arg)

How can g know if it is called directly with (('foo', 'bar'),) or via f
with ('foo', 'bar'). I coud write in f: return g(arg[0], arg[1]) if I
know the number of arguments, but what if I don't know that in design time?

TV
Nov 5 '06 #6
Tuomas wrote:
def g(*arg):
return arg

def f(*arg):
return g(arg)

How can g know if it is called directly with (('foo', 'bar'),) or via f
with ('foo', 'bar'). I coud write in f: return g(arg[0], arg[1]) if I
know the number of arguments, but what if I don't know that in design time?
So it seems that I would like to have an unpack operator:

def f(*arg):
return(!*arg)

TV
Nov 5 '06 #7
Tuomas schrieb:
Tuomas wrote:
>def g(*arg):
return arg

def f(*arg):
return g(arg)

How can g know if it is called directly with (('foo', 'bar'),) or via
f with ('foo', 'bar'). I coud write in f: return g(arg[0], arg[1]) if
I know the number of arguments, but what if I don't know that in
design time?


So it seems that I would like to have an unpack operator:

def f(*arg):
return(!*arg)

TV
Either you take one of the snippets here:
http://aspn.activestate.com/ASPN/sea...ype=Subsection
or just use arg[0] clever (as mentioned a few times in this thread).
Nov 5 '06 #8
Stargaming wrote:
Either you take one of the snippets here:
http://aspn.activestate.com/ASPN/sea...ype=Subsection

or just use arg[0] clever (as mentioned a few times in this thread).
Thanks. My solution became:
>>def flattern(arg):
.... result = []
.... for item in arg:
.... if isinstance(item, (list, tuple)):
.... result.extend(flattern(item))
.... else:
.... result.append(item)
.... return tuple(result)
....
>>def g(*arg):
.... arg = flattern(arg)
.... return arg
....
>>def f(*arg):
.... return g(arg)
....
>>f('foo', 'bar')
('foo', 'bar')

TV
Nov 5 '06 #9
Dennis Lee Bieber wrote:
On Sun, 05 Nov 2006 17:42:30 GMT, Tuomas <tu***************@pp.inet.fi>
declaimed the following in comp.lang.python:
>>I am looking a shorter way to do the above in the case:

def g(*arg):
return arg

def f(*arg):
return g(arg)

How can g know if it is called directly with (('foo', 'bar'),) or via f


Typically, the responsibility should be on the CALLER, not the
CALLED..

>>>>def g(*arg):

... return arg
...
>>>>def f(*arg):

... return g(*arg) #<<<<<<<< unpack tuple on call
...
>>>>f("a", 1, 2)

('a', 1, 2)
Note how f() is calling g() using an * -- Since f() "knows" that its
arguments were "packed" it calls g() with an unpack marker. Then g()
gets the arguments via whatever scheme it was coded to use.

>>>>def f(*arg):

... return g(arg) #<<<<<<<<<< no tuple unpack
...
>>>>f("a", 1, 2)

(('a', 1, 2),)

I fylly agree with tis: "Typically, the responsibility should be on the
CALLER, not the CALLED..". I just don't know how to unpack *arg for
calling g. I can get the len(arg), but how to formulate an unpacked call
g(arg[0], arg[1], ..). Building a string for eval("g(arg[0], arg[1],
...)") seems glumsy to me.

TV
Nov 5 '06 #10
On Sun, 05 Nov 2006 19:35:58 +0000, Tuomas wrote:
Thanks. My solution became:
>>def flattern(arg):
... result = []
... for item in arg:
... if isinstance(item, (list, tuple)):
... result.extend(flattern(item))
... else:
... result.append(item)
... return tuple(result)
...
>>def g(*arg):
... arg = flattern(arg)
... return arg
...
>>def f(*arg):
... return g(arg)
...
>>f('foo', 'bar')
('foo', 'bar')

That's the most complicated do-nothing function I've ever seen. Here is a
shorter version:

def shortf(*args):
return args

>>f('foo', 'bar')
('foo', 'bar')
>>shortf('foo', 'bar')
('foo', 'bar')
>>f(1,2,3,4)
(1, 2, 3, 4)
>>shortf(1,2,3,4)
(1, 2, 3, 4)
>>f({}, None, 1, -1.2, "hello world")
({}, None, 1, -1.2, 'hello world')
>>shortf({}, None, 1, -1.2, "hello world")
({}, None, 1, -1.2, 'hello world')

Actually, they aren't *quite* identical: your function rips lists apart,
which is probably not a good idea.
>>f("foo", [1,2,3], None) # three arguments turns into five
('foo', 1, 2, 3, None)
>>shortf("foo", [1,2,3], None) # three arguments stays three
('foo', [1, 2, 3], None)

I still don't understand why you are doing this. Can we have an example of
why you think you need to do this?

--
Steven.

Nov 6 '06 #11
Dennis Lee Bieber wrote:
On Sun, 05 Nov 2006 22:51:00 GMT, Tuomas <tu***************@pp.inet.fi>
declaimed the following in comp.lang.python:

>>>

I fylly agree with tis: "Typically, the responsibility should be on the
CALLER, not the CALLED..". I just don't know how to unpack *arg for
calling g. I can get the len(arg), but how to formulate an unpacked call
g(arg[0], arg[1], ..). Building a string for eval("g(arg[0], arg[1],
..)") seems glumsy to me.

Did you miss the example I gave? Using "*args" on the "def"
essentially says "pack remaining arguments into one tuple". Using
"*args" on a CALL says "UNPACK tuple into positional arguments"

def f(*args): <<<< pack arguments into tuple
Thats it:
x = g(*args) >>>unpack args tuple when calling
Yesterday I tested something like this and got a syntax error. So I got
misunderstanding that "g(*args)" is'nt a proper syntax. Obviously my
test sentence had some other syntax error. Sorry.

TV
Nov 6 '06 #12
Steven D'Aprano wrote:
On Sun, 05 Nov 2006 19:35:58 +0000, Tuomas wrote:

>>Thanks. My solution became:
>>def flattern(arg):
... result = []
... for item in arg:
... if isinstance(item, (list, tuple)):
... result.extend(flattern(item))
... else:
... result.append(item)
... return tuple(result)
...
>>def g(*arg):
... arg = flattern(arg)
... return arg
...
>>def f(*arg):
... return g(arg)
...
>>f('foo', 'bar')
('foo', 'bar')

That's the most complicated do-nothing function I've ever seen. Here is a
shorter version:

def shortf(*args):
return args
>>>>f('foo', 'bar')

('foo', 'bar')
>>>>shortf('foo', 'bar')

('foo', 'bar')

>>>>f(1,2,3,4)

(1, 2, 3, 4)
>>>>shortf(1,2,3,4)

(1, 2, 3, 4)

>>>>f({}, None, 1, -1.2, "hello world")

({}, None, 1, -1.2, 'hello world')
>>>>shortf({}, None, 1, -1.2, "hello world")

({}, None, 1, -1.2, 'hello world')

Actually, they aren't *quite* identical: your function rips lists apart,
which is probably not a good idea.

>>>>f("foo", [1,2,3], None) # three arguments turns into five

('foo', 1, 2, 3, None)
>>>>shortf("foo", [1,2,3], None) # three arguments stays three

('foo', [1, 2, 3], None)

I still don't understand why you are doing this. Can we have an example of
why you think you need to do this?
If i redefine the function g the difference comes visible:
>>def g(*arg):
.... if with_flattern: arg=flattern(arg)
.... return arg
>>with_flattern=False
f('foo', 'bar')
(('foo', 'bar'),)
>>with_flattern=True
f('foo', 'bar')
If you read the whole chain you find out what we were talking of.

TV
Nov 6 '06 #13
Tuomas wrote:
Steven D'Aprano wrote:
>>On Sun, 05 Nov 2006 19:35:58 +0000, Tuomas wrote:
>>>Thanks. My solution became:
>>def flattern(arg):

... result = []
... for item in arg:
... if isinstance(item, (list, tuple)):
... result.extend(flattern(item))
... else:
... result.append(item)
... return tuple(result)
...

>>def g(*arg):

... arg = flattern(arg)
... return arg
...

>>def f(*arg):

... return g(arg)
...

>>f('foo', 'bar')

('foo', 'bar')

That's the most complicated do-nothing function I've ever seen. Here is a
shorter version:

def shortf(*args):
return args

>>>>>f('foo', 'bar')

('foo', 'bar')

>>>>>shortf('foo', 'bar')

('foo', 'bar')
>>>>>f(1,2,3,4)

(1, 2, 3, 4)

>>>>>shortf(1,2,3,4)

(1, 2, 3, 4)
>>>>>f({}, None, 1, -1.2, "hello world")

({}, None, 1, -1.2, 'hello world')

>>>>>shortf({}, None, 1, -1.2, "hello world")

({}, None, 1, -1.2, 'hello world')

Actually, they aren't *quite* identical: your function rips lists apart,
which is probably not a good idea.
>>>>>f("foo", [1,2,3], None) # three arguments turns into five

('foo', 1, 2, 3, None)

>>>>>shortf("foo", [1,2,3], None) # three arguments stays three

('foo', [1, 2, 3], None)

I still don't understand why you are doing this. Can we have an example of
why you think you need to do this?


If i redefine the function g the difference comes visible:
>>def g(*arg):
.... if with_flattern: arg=flattern(arg)
.... return arg
>>with_flattern=False
>>f('foo', 'bar')
(('foo', 'bar'),)
>>with_flattern=True
>>f('foo', 'bar')

If you read the whole chain you find out what we were talking of.

TV
Suppose you did actually want to do this you have chosen about the worst
possible way: the use of global variables to condition function
execution is a sure way to get into trouble. Consider if somebody else
want to use your function: they also have to set a global in their
program to avoid your function raising an exception.

Fortunately Python has just the thing to make such horrors unnecessary:
the default argument value. Try something like this (untested):

def g(flattening=True, *arg):
if flattening:
arg = flatten(arg)
return arg

Obviously you could use either True or False for the default value. In
the case above the function flattens by default. You could also, if you
wished, have f() take the flattening argument, and always pass it to g().

Nothing very sophisticated here, just something to help you flex your
growing Python and programming muscles.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 6 '06 #14
Steve Holden wrote:
Suppose you did actually want to do this you have chosen about the worst
possible way: the use of global variables to condition function
execution is a sure way to get into trouble. Consider if somebody else
want to use your function: they also have to set a global in their
program to avoid your function raising an exception.
Do you think all discussion examples are included a producton application.
Fortunately Python has just the thing to make such horrors unnecessary:
the default argument value. Try something like this (untested):

def g(flattening=True, *arg):
if flattening:
arg = flatten(arg)
return arg

Obviously you could use either True or False for the default value. In
the case above the function flattens by default. You could also, if you
wished, have f() take the flattening argument, and always pass it to g().

Nothing very sophisticated here, just something to help you flex your
growing Python and programming muscles.
Thanks for your good purposes.

TV
regards
Steve
Nov 6 '06 #15
On Mon, 06 Nov 2006 13:03:55 +0000, Tuomas wrote:
If you read the whole chain you find out what we were talking of.
I had already read the whole thread, and I've read it again in case I
missed something the first time, and I still have no idea why you think
you need to do this. You explain *what* you want to do, but not *why* you
want to do it.
--
Steven.

Nov 7 '06 #16
Steven D'Aprano wrote:
On Mon, 06 Nov 2006 13:03:55 +0000, Tuomas wrote:

>>If you read the whole chain you find out what we were talking of.


I had already read the whole thread, and I've read it again in case I
missed something the first time, and I still have no idea why you think
you need to do this. You explain *what* you want to do, but not *why* you
want to do it.
Redirecting a funtion parameter to an other function is a quite common
situation in programming. Here the question was forwarding *args
parameter. Have you ever tried this and found the difference in cases:

def f(*args):
x = g(args) # how g sees arguments in this case
x = g(*args) # and how it sees them in this case

I am happy with what Dennis Lee Bieber wrote in the second latest node
in this chain. So luckily my "flattern" is not needed because the
problem I had can be solved in the calling function.

TV
Nov 7 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aa | last post by:
Anybody know the solution to trailing space in argument of Runtime.getRuntime().exec(arg), Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Documents and...
7
by: Robert Brown | last post by:
Is there a way to use PreparedStatements (or bind variables) with SQL statements that have a variable number of arguments. For example, I have an array of IDs for employees of a certain type and I...
1
by: Binod Nair | last post by:
Hi All, I have an ASP.NET appication running on http://xx.xxx.xxxx.xx/aspApplication and I have a domain http://www.mydomain.com registered at godaddy.com for this application.I have setup...
15
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main':...
0
by: nicholas | last post by:
I use session variables that stores data for a shopping basket. The site runs at www.mysite.com/france and works fine. But I also have the domainname: www.mysiteforfrance.com So, on a Plesk...
3
by: GS | last post by:
I have this: Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc As HtmlDocument = WebBrowser1.Document) As String flagged by vb as Error 5 Reference to a non-shared member...
5
by: Dave | last post by:
Hi All, I'm experiencing problems with sending mail using mail() to forwarded email accounts. The problem seems to revolve around the optional 4th argument of mail(), namely 'additional headers'....
6
by: mcl | last post by:
I have a domain name which is set up for web forwarding with a frame. I have a link on one of the site's pages to an external site. When I select the link the external site is displayed correctly...
2
dmjpro
by: dmjpro | last post by:
I am forwarding the error page using error-page tag in web.xml. If i want to pass some parameters with error_page .....My Code is somthing like that <error-page>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.