In Python is it better practice to write functions that accept tuples instead of lists if the data passed should be unmodified? Similar to how passing arguments by const reference in C++
makes it clear to the user that data won't be modified.
Asked
Active
Viewed 1,145 times
0

Matthew Feickert
- 786
- 6
- 26
-
11. How are you going to enforce that? 2. The data *inside* the tuple may still be modified inside the function so I don't see the point `def foo(data): ; data[0][0] = 1 ; print(data) ; foo(([0], 1)) ; # ([1], 1)` – DeepSpace Nov 13 '17 at 10:09
-
No. Usually tuples are used for things that have *structure*, whereas lists are used for things that have *order*. – Willem Van Onsem Nov 13 '17 at 10:12
-
1Your function generally shouldn't care if it gets a tuple or list. OTOH, it's a good idea to use tuples unless you need mutability. Also, lists generally shouldn't be used for non-homogeneous data, or collections where there's special significance to the positions of the items, eg (latitude, longitude, altitude) ought to be a tuple. Thus if it doesn't make sense to sort or otherwise re-order the collection it probably should be a tuple. – PM 2Ring Nov 13 '17 at 10:14
-
@PM2Ring `"it's a good idea to use tuples unless you need mutability"` tuples won't protect arbitrary objects from being modified. – DeepSpace Nov 13 '17 at 10:17
-
1@DeepSpace I didn't intend to imply that they do. The tuple itself is immutable, but it may contain mutable items, and nothing prevents those items from being mutated. – PM 2Ring Nov 13 '17 at 10:18
-
@PM2Ring correct, but OP asks for the sole sake of immutability. – DeepSpace Nov 13 '17 at 10:20
-
Does [the link in René Pijl's answer](https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples) address your question adequately? If so we can close this question as a duplicate of that one. – PM 2Ring Nov 13 '17 at 10:20
-
@PM2Ring, it doesn't exactly answer my question, but I think it is close enough that once my followup question in the comments below is addressed it can be closed. I'll let you be the judge on if it can be marked as a duplicate. – Matthew Feickert Nov 13 '17 at 10:38
1 Answers
0
No, tuples and lists are different things. Tuples have strucure, lists have order. Use each for its own purpose.

René Pijl
- 4,310
- 1
- 19
- 25
-
Okay, so I think this somewhat clarifies things. Given this --- that tuples should represent heterogeneous data and lists homogeneous --- is there a widely accepted way to broadcast to users that information in a passed list (e.g., bin edges of a histogram) will remain untouched? – Matthew Feickert Nov 13 '17 at 10:38
-
@MatthewFeickert Not exactly. However, it's a Python convention that functions don't have side effects, and if a function can modify its mutable args the docs for the function (and hopefully its name) should make that clear. On a related note, it's conventional for functions which modify their list (or other mutable) arg in-place return `None`. Of course it's legitimate to have a function that (say) takes in a list and _doesn't_ modify it, but still returns `None` (eg it may save the list to disk, plot it, etc), but in those scenarios I'd be inclined to return some kind of success indicator. – PM 2Ring Nov 13 '17 at 10:52
-
@PM2Ring Thanks for the nice summary. I think this can be closed now. – Matthew Feickert Nov 13 '17 at 10:55