Method/Function Signature Type checking

Erik Max Francis max at alcyone.com
Tue Aug 13 20:20:28 CEST 2002


Adam Gent wrote:
> 
> I'm almost certain that this has been brought up in the newsgroup
> before but I can't seem to google it (find it). I like python very
> much but was wondering if there is any intrest for dynamic type
> checking in the method/function signature. Im not talking about
> compile time/static type checking!

You're asking for all the benefits of static typing but without the
compile-tile advantages :-).

What you're asking for essentially is counter to the Python philosophy
(it's not "Pythonic").  The better way to handle such situations in
Python is assume that when you're passed an object that is going to be
used in some way, and just use it in that way.  If it is not as
advertised, then errors will occur.

This way you're concentrating on the behavior of the object, rather than
the precise nature of the object.  After all, if in your example you
were to limit yourself to strings, that would preclude allowing any
other string-like object, even though there's no real reason why these
shouldn't be allowed.  If it walks like a duck and quacks like a duck
...

It's true that I will on an occasion use an assert like

	assert isinstance(argument, Class)

but this is generally only early in a project where data structures must
be arranged in a certain way, and although any object that fits the
interface would work, at the time there is only one correct one and I'm
just trying to save debugging time later by finding mistakes now.  This
kind of thing really shouldn't be present in large scale APIs, though.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list