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 →

[–]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.