isinstance() considered harmful

Jason Orendorff jason at jorendorff.com
Wed Jan 23 20:36:44 EST 2002


Kragen Sitaker wrote:
> isinstance() considered harmful

I've been thinking about this, and I'd be a lot happier
with it if:

 (a) code using "try: ... except AttributeError:" were even
     remotely as clear as code using isinstance();
 (b) it left room for writing much faster Python code in a
     future version, the way explicit interfaces do; and
 (c) there were a way to make sure that "x.seek" is really
     a file.seek() method, and not something else.

I like the potential performance upside of static typing in
Python.

I wish I could say "if supports(object, wfile.write): ..."
instead of "if hasattr(object, 'write'): ..."
or the equivalent try/except/else incantation.

(In this pipe dream, wfile is a "builtin interface" that has
descriptors for write, writelines, flush, close, seek/tell,
softspace, and so on.)

Note also that the distutils policy of requiring extensions
to subclass Distribution has an important purpose: to ensure
that future versions of distutils can add methods to
Distribution, and Command can then safely use those methods.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list