File Doc/reference/expressions.rst changed (mode: 100644) (index f4a82699b0..d80768ac07) |
... |
... |
Membership test operations |
1431 |
1431 |
-------------------------- |
-------------------------- |
1432 |
1432 |
|
|
1433 |
1433 |
The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in |
The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in |
1434 |
|
s`` evaluates to true if *x* is a member of *s*, and false otherwise. ``x not |
|
1435 |
|
in s`` returns the negation of ``x in s``. All built-in sequences and set types |
|
1436 |
|
support this as well as dictionary, for which :keyword:`in` tests whether the |
|
1437 |
|
dictionary has a given key. For container types such as list, tuple, set, |
|
1438 |
|
frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent |
|
|
1434 |
|
s`` evaluates to ``True`` if *x* is a member of *s*, and ``False`` otherwise. |
|
1435 |
|
``x not in s`` returns the negation of ``x in s``. All built-in sequences and |
|
1436 |
|
set types support this as well as dictionary, for which :keyword:`in` tests |
|
1437 |
|
whether the dictionary has a given key. For container types such as list, tuple, |
|
1438 |
|
set, frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent |
1439 |
1439 |
to ``any(x is e or x == e for e in y)``. |
to ``any(x is e or x == e for e in y)``. |
1440 |
1440 |
|
|
1441 |
|
For the string and bytes types, ``x in y`` is true if and only if *x* is a |
|
|
1441 |
|
For the string and bytes types, ``x in y`` is ``True`` if and only if *x* is a |
1442 |
1442 |
substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are |
substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are |
1443 |
1443 |
always considered to be a substring of any other string, so ``"" in "abc"`` will |
always considered to be a substring of any other string, so ``"" in "abc"`` will |
1444 |
1444 |
return ``True``. |
return ``True``. |
1445 |
1445 |
|
|
1446 |
1446 |
For user-defined classes which define the :meth:`__contains__` method, ``x in |
For user-defined classes which define the :meth:`__contains__` method, ``x in |
1447 |
|
y`` is true if and only if ``y.__contains__(x)`` is true. |
|
|
1447 |
|
y`` returns ``True`` if ``y.__contains__(x)`` returns a true value, and |
|
1448 |
|
``False`` otherwise. |
1448 |
1449 |
|
|
1449 |
1450 |
For user-defined classes which do not define :meth:`__contains__` but do define |
For user-defined classes which do not define :meth:`__contains__` but do define |
1450 |
|
:meth:`__iter__`, ``x in y`` is true if some value ``z`` with ``x == z`` is |
|
|
1451 |
|
:meth:`__iter__`, ``x in y`` is ``True`` if some value ``z`` with ``x == z`` is |
1451 |
1452 |
produced while iterating over ``y``. If an exception is raised during the |
produced while iterating over ``y``. If an exception is raised during the |
1452 |
1453 |
iteration, it is as if :keyword:`in` raised that exception. |
iteration, it is as if :keyword:`in` raised that exception. |
1453 |
1454 |
|
|
1454 |
1455 |
Lastly, the old-style iteration protocol is tried: if a class defines |
Lastly, the old-style iteration protocol is tried: if a class defines |
1455 |
|
:meth:`__getitem__`, ``x in y`` is true if and only if there is a non-negative |
|
|
1456 |
|
:meth:`__getitem__`, ``x in y`` is ``True`` if and only if there is a non-negative |
1456 |
1457 |
integer index *i* such that ``x == y[i]``, and all lower integer indices do not |
integer index *i* such that ``x == y[i]``, and all lower integer indices do not |
1457 |
1458 |
raise :exc:`IndexError` exception. (If any other exception is raised, it is as |
raise :exc:`IndexError` exception. (If any other exception is raised, it is as |
1458 |
1459 |
if :keyword:`in` raised that exception). |
if :keyword:`in` raised that exception). |