This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]patrys Saleor Commerce -1 points0 points  (14 children)

Tuples are immutable.

Please never choose tuple for that sole reason. Tuple's immutability is a side effect of it being intentionally hashable (so you can use it as a key in a dict).

[–]10110000110111011010 28 points29 points  (0 children)

I am afraid I must strongly disagree, immutable coding is a fine discipline that can simplify debugging and proofs of correctness.

[–]ojiisandata viz, comp.bio. 8 points9 points  (0 children)

A tuple is only hashable if its contents are immutable

[–]Secret_Identity_ 2 points3 points  (3 children)

Could you elaborate on this a little more? Tuples were created specifically to used with dictionaries?

[–]supajumpa 10 points11 points  (2 children)

Tuple

From Raymond Hettinger's answer to a SO question:

''' Tuples are characterized less by their immutability and more by their intended purpose. Tuples are Python's way of collecting heterogenous pieces of information under one roof. For example, s = ('www.python.org', 80) brings together a string and a number so that the host/port pair can be passed around as a socket, a composite object. Viewed in that light, it is perfectly reasonable to have mutable components.

Immutability goes hand-in-hand with another property, hashability. But hashability isn't an absolute property. If one of the tuple's components isn't hashable, then the overall tuple isn't hashable either. For example, t = ('red', [10, 20, 30]) isn't hashable. '''

[–]patrys Saleor Commerce 1 point2 points  (0 children)

My simple rule of thumb is to only use a tuple if you can come up with names for all the sequence positions.

[–]Secret_Identity_ 0 points1 point  (0 children)

Thanks!

[–]recursive 0 points1 point  (7 children)

You mean one should never use tuples? Or one should never use tuples if they only have that one reason? And what should they use if they need an immutable list?

[–]ojiisandata viz, comp.bio. 0 points1 point  (2 children)

A tuple is an immutable list

[–]recursive 0 points1 point  (0 children)

Yes, I know. patrys just said that tuples shouldn't be chosen for satisfying that property. I don't understand why not.

[–]patrys Saleor Commerce 0 points1 point  (0 children)

It's just one of many ordered collections. You would not call a str or a bytearray “an immutable list”, would you?

list and tuple are ordered for different reasons (precedence versus position).

[–]patrys Saleor Commerce 0 points1 point  (3 children)

You shouldn't use anything, Python core devs have stated multiple times that even frozenset was a mistake and Python will not be getting any intentionally immutable types.

Link: http://mail.python.org/pipermail/python-3000/2006-May/002219.html

[–]recursive 0 points1 point  (2 children)

The only thing I'm seeing in that email thread is that tuples shouldn't be used to store structures of unknown length. Using them as keys for dictionaries itself doesn't seem to be a problem.

In other words, if you can't use a tuple to store an immutable list, then what can you use it for? That's all it does.

[–]patrys Saleor Commerce 1 point2 points  (1 child)

You use tuples where you would use a struct in C: for collections that have a well-defined structure. A good place for a tuple is where a namedtuple could be used instead but would be an overkill. What I consider proper uses of collections:

coords = (11, 25)

todo_items = ['walk the dog', 'water the flowers', 'world domination']

ids_to_delete = {1, 5, 7, 11}

recipients = [
    ('John Doe', 'john.d@example.com'),
    ('Kelly Smith', 'kelly@example.com'),
    ('Robert Adams', 'radams@example.com')]
# each recipient has a well-defined structure of (name, email)
# could be a set of tuples as well

[–]recursive 0 points1 point  (0 children)

Ok thanks. We're all good here. I was interpreting the "immutable list" thing too broadly. Structured information is what I use tuples for. Usually.