Type checking in python?

Thomas Wouters thomas at xs4all.net
Tue Jul 18 17:10:47 EDT 2000


On Tue, Jul 18, 2000 at 09:29:31AM -0600, Eric Hopper wrote:

[ interfaces ]

> > In general, the Python philosophy is to "just try it" and see if it
> > works. Sometimes type checks are appropriate but if you do it on the
> > entry to every function, you are probably doing too much work and making
> > your own life (or someone else's) harder later on.
> 
> Oh, yes.  Inheritance by mysteriously coiniciding method names.  How fun.

Nono, it's not inheritance. It's *interfaces* :) Inheritance is done the
plain old way, well known to those used to OO languages.

> Just accepting any old type, and letting it error out when it tries to
> access a non-existent attribute is very confusing for people trying to use
> your code.  Heck, maybe you store away their object, and then call the
> offending method at some random later point.  I bet it'd be a heck of a
> lot of fun to try to trace down why the exception happened then.

Well, I've more than once written a debug object that behaved like another
object but with debug output. Without subclassing, because I would've had to
override all methods anyway, and I didn't want the debug class to *do*
anything.

If you insist on type-checking, do it the interface-way:

getattr(object, "required_method")
getattr(object, "required_datatype")
[etc]

Though I'd encourage you to add some docstrings and consider your users
smart enough to read the docstrings. Most python programmers are, really ;)

> Type checking and the scoping rules are the only two problems I have with
> Python so far.  Other than that, it's a wondefrul language.

Scoping is incredibly simple in Python. There's the local scope, and the
global scope ;) It might take some getting used to if you're used to magic
or explicit scopes, but it's definately very simple ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list