Python: Why does str.split () return a list while str.partition () returns a tuple?

Comparing Python str.split() with str.partition() , I see that they not only have different functions ( split() tokenizes the entire line at each occurrence of the separator, and partition() just returns everything earlier and everything after the first delimitation) , but they also have different types of data returned. That is, str.split() returns a list , and str.partition() returns a tuple . This is important because a list is mutable and a tuple is not. Is there any deliberate reason for this choice in the design of the API, or is it "as it is." I am curious.

+6
source share
1 answer

The key difference between these methods is that split() returns a variable number of results, and partition() returns a fixed number. Tuples are not commonly used for APIs that return a variable number of elements.

+9
source

Source: https://habr.com/ru/post/984691/


All Articles