summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-03-06 07:31:34 (GMT)
committerGeorg Brandl <georg@python.org>2008-03-06 07:31:34 (GMT)
commit6c45dc12b3232b1c709869f667cb6bcd54486a29 (patch)
treeb50fa649ff0e067ea464c4583e44e7e3890b2e80
parent26bab5f92a9ec67647a1893dfc497d822c1f62bd (diff)
downloadcpython-6c45dc12b3232b1c709869f667cb6bcd54486a29.zip
cpython-6c45dc12b3232b1c709869f667cb6bcd54486a29.tar.gz
cpython-6c45dc12b3232b1c709869f667cb6bcd54486a29.tar.bz2
Add isinstance/issubclass to tutorial.
-rw-r--r--Doc/tutorial/classes.rst16
1 files changed, 16 insertions, 0 deletions
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst
index 2eb86e2..dddbb0c 100644
--- a/Doc/tutorial/classes.rst
+++ b/Doc/tutorial/classes.rst
@@ -420,6 +420,9 @@ classes defined in it. Usually, the class containing the method is itself
defined in this global scope, and in the next section we'll find some good
reasons why a method would want to reference its own class!
+Each value is an object, and therefore has a *class* (also called its *type*).
+It is stored as ``object.__class__``.
+
.. _tut-inheritance:
@@ -469,6 +472,19 @@ arguments)``. This is occasionally useful to clients as well. (Note that this
only works if the base class is defined or imported directly in the global
scope.)
+Python has two builtin functions that work with inheritance:
+
+* Use :func:`isinstance` to check an object's type: ``isinstance(obj, int)``
+ will be ``True`` only if ``obj.__class__`` is :class:`int` or some class
+ derived from :class:`int`.
+
+* Use :func:`issubclass` to check class inheritance: ``issubclass(bool, int)``
+ is ``True`` since :class:`bool` is a subclass of :class:`int`. However,
+ ``issubclass(unicode, str)`` is ``False`` since :class:`unicode` is not a
+ subclass of :class:`str` (they only share a common ancestor,
+ :class:`basestring`).
+
+
.. _tut-multiple: