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

Tuples in function argument lists

I'm trying to understand the use of tuples in function argument lists.

I did this:
def tester(a, (b,c)): .... print a, b, c
.... tester(1, 2) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in tester
TypeError: unpack non-sequence

That was obvious result.
tester(1, (2, 3)) 1 2 3 tester('ab', 'ab') ab a b

And so were those.

Then I tried this:
def tester(a, (b,c)=None):

.... if (b,c) is None:
.... print a, None
.... else:
.... print a, b, c

Needless to say, it did not do what I expected it to do. I didn't expect
it to either :-)

I tried looking at the language reference here:

http://docs.python.org/ref/function.html

but I can't seem to find anything in their that says that tuples-as-args
are legal. Am I misreading the docs, or is this accidental behaviour that
shouldn't be relied on?

Does anyone use this behaviour, and if so, under what circumstances is it
useful?
--
Steven
Jul 21 '05 #1
1 1589
Steven D'Aprano wrote:
I'm trying to understand the use of tuples in function argument lists.

I did this:
def tester(a, (b,c)):
... print a, b, c
...
tester(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in tester
TypeError: unpack non-sequence

That was obvious result.
tester(1, (2, 3))
1 2 3
tester('ab', 'ab')
ab a b

And so were those.

Then I tried this:
def tester(a, (b,c)=None):

... if (b,c) is None:
... print a, None
... else:
... print a, b, c

Needless to say, it did not do what I expected it to do. I didn't expect
it to either :-)

I tried looking at the language reference here:

http://docs.python.org/ref/function.html

but I can't seem to find anything in their that says that tuples-as-args
are legal. Am I misreading the docs, or is this accidental behaviour that
shouldn't be relied on?


Tuples-as-arg are legal. Tuple-as-keyword, too *if* the default value is
something that can actually unpack to a tuple. A default of None is...silly.

In [6]: def tester(a, (b,c)=(1,2)):
...: print a,b,c
...:

In [7]: tester(1)
1 1 2

Tuple-as-arg is probably pretty safe. Tuple-as-keyword, possibly
not-so-much.
Does anyone use this behaviour, and if so, under what circumstances is it
useful?


import math
def distance((x1,y1), (x2,y2)):
return math.sqrt((x2-x1)**2 + (y2-y1)**2)
distance(point1, point2)

Personally, I prefer to explicitly unpack the tuple in the function body.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 21 '05 #2

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

Similar topics

10
by: Ivan Voras | last post by:
Are there any performance/size differences between using tuples and using lists? -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
2
by: beliavsky | last post by:
Tuples, unlike lists, are immutable, which I crudely translate to mean "their contents cannot be changed". Out of habit, I use only lists, not tuples, in my code. But I wonder if this is poor style...
3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
14
by: Richard | last post by:
I have a large list of two element tuples. I want two separate lists: One list with the first element of every tuple, and the second list with the second element of every tuple. Each tuple...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
8
by: MartinRinehart | last post by:
What are the considerations in choosing between: return and return (a, b, c) # or return a, b, c Why is the immutable form the default?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.