|
From: Joerg L. <jo...@us...> - 2004-07-27 16:25:37
|
Hi Magnus,
On 27.07.04, Magnus Lie Hetland wrote:
> Just a general comment on the use of isinstance -- I think it's best
> avoided when possible. Type checking breaks polymorphism... Signature
> checking or the "leap before you look" paradigm are, IMO, more
> Pythonic. Just noticed a few isintance calls in the code here and
> there -- just thought I'd pipe in. Feel free to ignore :)
In general, you're right - and there are probably some places where
avoiding isinstance would be better. However, there are some
places where this is not so easy, and a simple isinstance is clearer
than code which tries to generalize even the special case.
Sometimes we also check for instances of a specific class to throw
an exception already quite early. In principle this is not necessary,
but believe me, it helps when you now early on that you did something
wrong...
Another typical use of isinstance is to implement some type based
polymorphism. For instance, if you pass some object to a function and
this function checks in which way he can deal with this object. Here, I
have to admit, I'm not always sure about the Python way of doing things
like that... A typical example was the constructor of the unit.length
class (only in the released PyX versions), which was able to convert
nearly anything to a length. In CVS we already got rid of that...
Jörg
|