polymorphism w/out signatures?

Gary Herron gherron at islandtraining.com
Fri May 7 14:53:51 EDT 2004


On Friday 07 May 2004 11:07 am, pugnatio2 at yahoo.com wrote:
> Thanks to all who replied. I think I've got it straight now, and by
> the way I did use "polymorphism" incorrectly; I should have said
> "method overloading."
>
> I guess I have my choice between separate callables and using type()
> or isinstance().
>
> What I had hoped to do was something like:
>
> xmlDoc = myFormat2XML(obj)
>
> where 'obj' could be either a text string or a sequence of lines. It's
> not just a problem of whether a scalar or sequence object is passed
> in. Strings, lists, and tuples are all sequences.
>
> 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.

Gary Herron








More information about the Python-list mailing list