Add isinstance/issubclass to tutorial.

This commit is contained in:
Georg Brandl 2008-03-06 07:31:34 +00:00
parent 26bab5f92a
commit 6c45dc12b3
1 changed files with 16 additions and 0 deletions

View 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: