8000
We read every piece of feedback, and take your input very seriously.
1 parent eee05c5 commit 79f054cCopy full SHA for 79f054c
Doc/faq/programming.rst
@@ -1906,28 +1906,30 @@ In the standard library code, you will see several common patterns for
1906
correctly using identity tests:
1907
1908
1) As recommended by :pep:`8`, an identity test is the preferred way to check
1909
-for ``None``. This reads like plain English in code and avoids confusion with
1910
-other objects that may have boolean values that evaluate to false.
+ for ``None``. This reads like plain English in code and avoids confusion
+ with other objects that may have boolean values that evaluate to false.
1911
1912
2) Detecting optional arguments can be tricky when ``None`` is a valid input
1913
-value. In those situations, you can create a singleton sentinel object
1914
-guaranteed to be distinct from other objects. For example, here is how
1915
-to implement a method that behaves like :meth:`dict.pop`::
+ value. In those situations, you can create a singleton sentinel object
+ guaranteed to be distinct from other objects. For example, here is how
+ to implement a method that behaves like :meth:`dict.pop`:
1916
1917
- _sentinel = object()
+ .. code-block:: python
1918
1919
- def pop(self, key, default=_sentinel):
1920
- if key in self:
1921
- value = self[key]
1922
- del self[key]
1923
- return value
1924
- if default is _sentinel:
1925
- raise KeyError(key)
1926
- return default
+ _sentinel = object()
+
+ def pop(self, key, default=_sentinel):
+ if key in self:
+ value = self[key]
+ del self[key]
+ return value
+ if default is _sentinel:
1927
+ raise KeyError(key)
1928
+ return default
1929
1930
3) Container implementations sometimes need to augment equality tests with
-identity tests. This prevents the code from being confused by objects such as
-``float('NaN')`` that are not equal to themselves.
1931
+ identity tests. This prevents the code from being confused by objects
1932
+ such as ``float('NaN')`` that are not equal to themselves.
1933
1934
For example, here is the implementation of
1935
:meth:`!collections.abc.Sequence.__contains__`::