Commit 6c45dc12 authored by Georg Brandl's avatar Georg Brandl

Add isinstance/issubclass to tutorial.

parent 26bab5f9
......@@ -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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment