[Python-Dev] Evil isinstance()

Patrick K. O'Brien pobrien@orbtech.com
Mon, 1 Apr 2002 16:56:37 -0600


[Guido van Rossum]

> > This doesn't seem so much of a problem to me to warrant rejecting
> > perfectly good numberlike and stringlike classes' instances, by
> > isinstance'ing away.
>
> Agreed, and I think the solution ought to be sought in asking a
> question involving hasattr().

Having recently removed most calls to hasattr() from PyCrust's introspection
routines, I'd be curious to get more of your opinion on this issue.
Specifically, I removed calls to hasattr() because xmlrpclib always returns
true for hasattr(), rendering it effectively meaningless.

>>> import xmlrpclib
>>> p = xmlrpclib.ServerProxy("http://betty.userland.com", verbose=1)
>>> hasattr(p, 'bogus')
1
>>> p.bogus
[big error message snipped]

I discussed this with Mr. Lundh and ultimately worked around this by
removing calls to hasattr(), but I'm still left feeling a bit uncomfortable
by this whole situation. Perhaps I was being naive, but I thought I could
rely on hasattr(). According to Mr. Lundh:

"Having an attribute doesn't mean that you carry out any possible operation
on this attribute. In this case, the server proxy will return a method proxy
for any possible attribute (hasattr and getattr always succeeds). You won't
get an error until you attempt to *call* the method proxy..."

This bit of advice seems to fall toward the opposite end of the spectrum
from the advice to look to hasattr() for a solution. So I figured I would
open this can of worms. I hope this isn't too off-topic.

---
Patrick K. O'Brien
Orbtech