473,378 Members | 1,360 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,378 software developers and data experts.

Detupleize a tuple for argument list

Hi,

I want to give a tuple to a function where the function
expects the respective tuple-size number of arguments.

The following session illustrates what I want to do and
the respective failure.

Python 2.4.1 (#7, Aug 3 2005, 14:55:58)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>def foo(a, b): print a, b
...
>>t = (1, 2)
def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b))
...
>>foo(1, 2)
a == 1, b == 2
>>foo(t)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 2 arguments (1 given)
>>>
One way to do what I want is--of course--to call
foo(t[0], t[1]). My actual question is if there is a
smarter way to do it.

The situation for me is that I take the functions from
a library that I cannot modify. On the other side in
my code I use the tuples.
Best wishes
--
Marco Wahl
http://visenso.com

Jul 5 '06 #1
4 1445
Marco Wahl wrote:
Hi,

I want to give a tuple to a function where the function
expects the respective tuple-size number of arguments.
....
One way to do what I want is--of course--to call
foo(t[0], t[1]). My actual question is if there is a
smarter way to do it.
Yes, just this:

foo(*t)

Luke

Jul 5 '06 #2
On Wed, 05 Jul 2006 14:01:27 +0200, Marco Wahl wrote:
Hi,

I want to give a tuple to a function where the function
expects the respective tuple-size number of arguments.

The following session illustrates what I want to do and
the respective failure.

Python 2.4.1 (#7, Aug 3 2005, 14:55:58)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>def foo(a, b): print a, b
...
>>t = (1, 2)
>>def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b))
...
>>foo(1, 2)
a == 1, b == 2
>>foo(t)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 2 arguments (1 given)
Easy: foo(*t)

--
Steve.

Jul 5 '06 #3
Marco Wahl enlightened us with:
>>foo(t)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 2 arguments (1 given)

Call foo(*t)
Thank you very much Luke Plant, Steven D'Aprano and Sybren Stuvel.

This was exactly what I was looking for. I'm happy now. ;-)
--
Marco Wahl
http://visenso.com
Jul 5 '06 #4
Marco Wahl wrote:
>>Marco Wahl enlightened us with:
>>>>>>foo(t)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 2 arguments (1 given)

Call foo(*t)


Thank you very much Luke Plant, Steven D'Aprano and Sybren Stuvel.

This was exactly what I was looking for. I'm happy now. ;-)
FWIW, there's something similar for dicts and named args:
>>def foo(a, b): print str(a), str(b)
....
>>kwargs = {'a' : 1, 'b': 2}
foo(**kwargs)
1 2
>>>


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 5 '06 #5

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

Similar topics

10
by: Carlo v. Dango | last post by:
Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
12
by: Kay Schluehr | last post by:
Hi all, thanks for Your attention ! I think my proposal was more in mind of Rons modified exec than Pythons lambda. When George proposed his unpacking behavoir for list-comps as a pack of...
3
by: James Stroud | last post by:
Hello All, Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some subtle logic? Why does print not work the same way as raise? Both are statements. Why does raise need to...
5
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence....
1
by: Giovanni Toffoli | last post by:
Hi, I'm not in the mailing list. By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002) of Jeff Shannon:...
7
by: Shafik | last post by:
Hello folks, I am an experienced programmer, but very new to python (2 days). I wanted to ask: what exactly is the difference between a tuple and a list? I'm sure there are some, but I can't...
4
by: Johannes Bauer | last post by:
Hello group, I'm currently doing something like this: import time localtime = time.localtime(1234567890) fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % (localtime, localtime, localtime,...
0
by: Steve Holden | last post by:
Hamish McKenzie wrote: Vector(1, 2, 3) fails for exactly the same reasons as list: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list() takes at most 1...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.