Type checking in python?

Samuel Scarano srs25 at cornell.edu
Thu Jul 20 10:29:41 EDT 2000


First of all, are you referring to something specific by "Unicode strings"?
(I wasn't aware of any standard unicode string class but I'd be interested
if there is one.)

Anyway, interesting point, but isn't that what multiple inheritance is for?
A static (or, for that matter, dynamic) typecheck ensuring that the function
is called with a string would work fine with unicode strings as long as the
unicode string class is a subclass of the string class.

Of course, you can't inherit from a built-in type, which is one of the
uglier features of Python, so it wouldn't work in this case. But is that the
only reason not to do it? That is, when dealing only with user-defined
classes, is there ever any real reason to try to fool a function by passing
an object of the wrong class but with the right methods, rather than
declaring its class as a subclass of the class the function is expecting?

Paul Prescod wrote:
> 
> Matthew Cline wrote:
> >
> >   def my_func(foo, bar, quux):
> >      if not isinstance(quux, Quux):
> >         raise RuntimeError("param 'quux' must be of class 'Quux'")
> 
> Let me also point out that in many cases, this style of programming
> would be frowned upon by serious Python programmers. For instance, if
> you check that something is a string, your code will complain when it is
> handed a Unicode string, even though it would probably work fine. If you
> check that it is an open file, then your code probably will complain
> about stringIO file-like objects, even though it would probably work
> fine. If you check that it is an integer, your code will complain about
> integers, even though ti would probably work fine.

-- 
Samuel R. Scarano                        "Due to circumstances beyond my
undergraduate, Cornell University        control, I am master of my fate
<srs25 at cornell.edu>                      and captain of my soul."
http://people.cornell.edu/pages/srs25/             -- Ashleigh Brilliant



More information about the Python-list mailing list