summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2021-04-05 19:48:24 (GMT)
committerGitHub <noreply@github.com>2021-04-05 19:48:24 (GMT)
commit7bc25ec7276db2a81e7823671a74eeb8aa6b4542 (patch)
tree1eea8fba066fc8a99c25e51bb9e275e12eea6125
parent75220674c07abfc90c2cd7862d04cfa2e2354450 (diff)
downloadcpython-7bc25ec7276db2a81e7823671a74eeb8aa6b4542.zip
cpython-7bc25ec7276db2a81e7823671a74eeb8aa6b4542.tar.gz
cpython-7bc25ec7276db2a81e7823671a74eeb8aa6b4542.tar.bz2
bpo-20503: Show how isinstance() works with ABC registered classes. (GH-25175)
-rw-r--r--Doc/faq/programming.rst35
1 files changed, 35 insertions, 0 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 93ffd36..514ca04 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1420,6 +1420,41 @@ single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
check whether an object is one of Python's built-in types, e.g.
``isinstance(obj, str)`` or ``isinstance(obj, (int, float, complex))``.
+Note that :func:`isinstance` also checks for virtual inheritance from an
+:term:`abstract base class`. So, the test will return ``True`` for a
+registered class even if hasn't directly or indirectly inherited from it. To
+test for "true inheritance", scan the :term:`MRO` of the class:
+
+.. testcode::
+
+ from collections.abc import Mapping
+
+ class P:
+ pass
+
+ class C(P):
+ pass
+
+ Mapping.register(P)
+
+.. doctest::
+
+ >>> c = C()
+ >>> isinstance(c, C) # direct
+ True
+ >>> isinstance(c, P) # indirect
+ True
+ >>> isinstance(c, Mapping) # virtual
+ True
+
+ # Actual inheritance chain
+ >>> type(c).__mro__
+ (<class 'C'>, <class 'P'>, <class 'object'>)
+
+ # Test for "true inheritance"
+ >>> Mapping in type(c).__mro__
+ False
+
Note that most programs do not use :func:`isinstance` on user-defined classes
very often. If you are developing the classes yourself, a more proper
object-oriented style is to define methods on the classes that encapsulate a