Make it immutable unless you've got a good reason not to.

For example, in , prefer tuples to lists.

@peterdrake

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?

@peterdrake

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.

@freemo Hmm. I'm not even sure how to write that function using a list. Is it done with type hints?

I wasn't aware that tuples and lists interacted with types differently.

Follow

@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.

@casastorta

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.

@peterdrake

@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.

@casastorta

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.

@peterdrake

@casastorta

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

@peterdrake

@casastorta
@peterdrake

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.

@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:
docs.python.org/3/library/typi

It's literally given as example:

```
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
...
```

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.