A little stricter type checking
Peter Otten
__peter__ at web.de
Sat Sep 4 13:05:38 EDT 2004
Andrew Dalke wrote:
[examples of isinstance() usage in the standard library]
> The only examples I found along the lines you wanted were
> in pickletools.py, copy.py, sets.py, and warnings.py.
> It just isn't needed that often in Python programming.
There are at least three more idioms that could (sometimes) be replaced
by type-checking:
# "casting"
s = str(s)
it = iter(seq)
# check for an "interface"
try:
lower = s.lower
except AttributeError:
raise TypeError
else:
lower()
# check for an "interface", LBYL-style
if not hasattr(foo, "bar"):
raise TypeError
These are more difficult to hunt and I'm lazy.
However, I don't think you can tell from the structure alone whether an
explicit type declaration would be a superior solution in these cases.
Should Python be extended to allow type declarations, I expect them to
appear in places where they reduce the usefulness of the code while
claiming to make it safe...
Peter
More information about the Python-list
mailing list