]> granicus.if.org Git - python/commitdiff
Add isinstance/issubclass to tutorial.
authorGeorg Brandl <georg@python.org>
Thu, 6 Mar 2008 07:31:34 +0000 (07:31 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 6 Mar 2008 07:31:34 +0000 (07:31 +0000)
Doc/tutorial/classes.rst

index 2eb86e29e1a9d8616f704675217c4396035d64e7..dddbb0c3fabae153df32b530687031a07c25e975 100644 (file)
@@ -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: