Why doesn't str.end allow the suffix parameter to be a list?

I see that the method str.endswithallows the parameter to suffixbe a tuple of strings :

Docstring:
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.

so I assumed that it also accepts a list of strings or other iterables , but when I tried this, passing the list causes an error:

In [300]: s='aaa'
In [301]: s.endswith(('a', 'b'))
Out[301]: True
In [302]: s.endswith(['a', 'b'])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-302-d70816089fed> in <module>()
----> 1 s.endswith(['a', 'b'])
TypeError: endswith first arg must be str, unicode, or tuple, not list

So why does it only accept a tuple of strings ?

+4
source share
3 answers

The API simply repeats isinstance()and except (Exception1, Exception2), which both accept only tuples.

See the original function request :

, , isinstance , str.startswith /.

, (). Python. , , , :

, , . , ( ).

+2

, str.startswith str.endswith tuples lists - : isinstance 21 2001 ( , Python 2.2).

startswith/ends :

, , isinstance , str.startswith endswith /.

+3

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


All Articles