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.

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

Follow

@freemo @peterdrake Is the origin of tuples and lists in Python; come from ML in this form

ยท ยท 0 ยท 0 ยท 0
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.