Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python's tuple not have any method?

Tags:

python

tuples

Why does Python's tuple not have any method associated with it? e.g. tuple.append(), tuple.remove(), etc.?

If the contents of tuple are accessed as we access list items, then why does tuple not have list-related methods?

like image 759
Aamir Rind Avatar asked Dec 01 '22 07:12

Aamir Rind


2 Answers

tuples are immutable, you cannot change their content.

EDIT - as commented on the question, and I'm not sure came clear from my answer - there are methods for tuples, but not methods that modify them.

like image 112
MByD Avatar answered Dec 05 '22 04:12

MByD


Tuples are immutable, whereas lists can be modified. This in itself explains why tuples don't have any way of modifying them, but implies another question... why are they immutable? what good is an immutable list? why not just have a single list type? It comes down to a couple of subtle design considerations...

To a degree, it provides a semantic convention: a tuple are supposed to be treated as a single "pairing" of a set of n objects. Typically, this is like a row in an SQL database... the position of an element in a tuple typically has an implicit meaning (id, name, etc), and the types of the tuple's elements may be vastly different (this idea was strengthened even further by the introduction of the namedtuple class in 2.6). This differs from the normal usage of a list, which usually contains a variable number of elements, all of the same type, and position implies little about an element besides ordering. Alternatively, they can be thought of as a snapshot of a list, much like frozenset vs set. (Not that any of this is enforced in any way - just that going against the grain may lead you to have problems like trying to modify a tuple).

So that's nice and all, but what actual good is it? For one thing, since tuples are immutable, they can't be changed once created. This lets you use them as keys in a dictionary, whereas a list might have it's contents changed, and thus can't be hashed.

This lets them be treated more as an anonymous pairing of objects, equal to any other identical pairing of the same set of objects, whereas lists may have hidden behaviors... if a reference to a list is stored somewhere, modifying the list could change a list of names displayed in a gui, or what have you. Thus two lists may have equal contents, but modifying them may have different behaviors in the overall program. Whereas when you're passing a tuple around, you know there's no way it will be modified out from under you... this particular choice has been set in stone. Thus, instead of modifying a tuple, you have to make a new tuple.

I don't think I captured all the use-cases, and don't worry if you don't quite get the intended use-cases just yet... the difference in intent of the two classes is subtle, but powerful, it may take some playing around with before it clicks.

like image 44
Eli Collins Avatar answered Dec 05 '22 03:12

Eli Collins