Make it immutable unless you've got a good reason not to.
For example, in #Python, prefer tuples to lists.
good advice. Though a frozen list is probably a better suggestion as a tuple is subtly different than a list.
@freemo I haven't really worked with frozen lists. Can you give an example where a tuple wouldn't suffice?
Quite a few differences, but for a concrete example here is one...
Write a python function that takes a single argument, a collection of integers of any length strictly containing integer type.. have the function add all the members of the collection together and return the value. Ensure that if the wrong type of value is passed in that it fails at **compile** time, not runtime.
If you use typles to represent immutable lists you cant do this. If you use frozen lists you can..
Worse yet you wont realize it is even a problem until you actually come across some function in a library that does this (or worse yet explicitly checks the type).. so you may have your whole app coded up before you realize you have to change all your tuples to lists..
So yea the proper way to do this is to feeze your lists and not consider a tuple interchangable with a list, they are not.
@peterdrake yes its done with type hints. There is no way with tupples using type hints to say "a tuple of any size where all members are integers".
@peterdrake Yea I learned that one the hard way a while ago.. Since tuples are builtin to python it looks like a natural solution... sadly it can bite you int he ass.
@freemo @peterdrake Is the origin of tuples and lists in Python; come from ML in this form
@freemo @peterdrake Huh,... Not sure if I misunderstand you but:
```
from typing import Tuple
def function(intup: Tuple[int, ...]):
print("%s" % (intup,))
```
would literally be it. Type hint `Tuple[int, ...]` literally means - tuple of any size, with all items being integers.
With that particular type hint you could pass in (5, "hello") since it only type checks the first entry and all other entries can be anything.
@freemo @peterdrake As far as Python, interpreter, is concerned - you can still pass anything there. Type hints are just that - runtime doesn't enforce anything. But any decent toolkit will read this as - tuple of any size with all items being int.
hmmm now i need to open an interprier.. wasnt that long ago i ran into that problem but it may have been becauseof the flavor of linter I was using.
Ok so just checked and the standard is explicitly written to accept it as a homogonous type if there is just one argument and the ellipsis
So to correct my earlier statement, the reason a frozen list is better is because its still a list.. If you have a function where the type hint requires it take a list then you can pass a mutable or immutable list to it, and cant pass a tuple. So they arent interchable as they have different types.
@freemo @casastorta Could you use Sequence as a type?
@peterdrake @freemo Not completely sure what's the ask, but along the same line as we've discussed before in this thread, according to Python doc - yes:
https://docs.python.org/3/library/typing.html#type-aliases
It's literally given as example:
```
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
...
```
@freemo Interesting!