polymorphism w/out signatures?

Donn Cave donn at u.washington.edu
Fri May 7 15:52:34 EDT 2004


In article <mailman.348.1083956045.25742.python-list at python.org>,
 Gary Herron <gherron at islandtraining.com> wrote:

> On Friday 07 May 2004 11:07 am, pugnatio2 at yahoo.com wrote:
...
> > The deprecation I read, about avoiding type() (p. 129 of _Python in a
> > Nutshell_) said it interfered with (true) polymorphism (via
> > inheritance).
> 
> That deprecation is correct.  Instead of using type(), use
> isinstance(param, type).
> 
> The call to isinstance returns True if param is an instance of the
> given type or any subtype thereof.  This works correctly for
> inheritance, giving you the polymorphism you want.

Subtype polymorphism!  Is that how it works in Python, though?

  >>> f = open('x', 'r')
  >>> import StringIO
  >>> s = StringIO.StringIO('contents ...')
  >>> t = type(f)
  >>> t 
  <type 'file'>
  >>> isinstance(s, t)
  0

Who cares if it's a subtype?

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list