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 1 1601
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it
than just that. Everything I tried with a list worked the same with a tuple. So, what's the
difference and why choose one over the other?
Jeff
|
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 -- putting
a collection of data in a tuple, when possible, makes it easier to
follow the code. You do not have to think about whether contents have
changed from where the tuple was first defined.
Is "use tuples instead of lists, when possible"...
|
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 and
lists is that tuples are immutable, so if this is correct, than list
are a superset of tuples, meaning lists can do everything tuples can
do and more.
Is there any advantage for using tuples? Are they "faster"? Consume
less memory? When is...
|
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 contains a datetime object followed by an integer.
Here is a small sample of the original list:
((datetime.datetime(2005, 7, 13, 16, 0, 54), 315),
(datetime.datetime(2005, 7, 13, 16, 6, 12), 313),
| |
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 values side. IIRC,
"apply" used to require that the second argument be a tuple; it now
accepts sequences, and has been depreciated in favor of *args, which
accepts not only sequences but iterators.
Is there any place in the language that still...
|
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 like this:
('eco', "(u'Roads',)", 0.073969887301348305)
Pysqlite doesn't like the format of the middle term:
pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
|
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 the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search
of the Library Reference's index seemed to confirm that the function did not
exist. A little later I realized it might be called...
|
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?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |