An Object's Type

Chris Mellon arkanes at gmail.com
Thu Dec 6 12:38:19 EST 2007


On Dec 6, 2007 5:52 AM, paul <paul at subsignal.org> wrote:
> Bruno Desthuilliers schrieb:
> > bg_ie at yahoo.com a écrit :
> >> Hi,
> >>
> >> Is it possible to find out if an object is of a certain type or of a
> >> type derived from this type?
> >>
> > You have the answer, thanks to Diez and Christian. Now unless you have a
> > *very* compelling reason to check the type of an object, *just forget
> > about it*. 9 times out of 10, this is fighting against the language's
> > type system (hint: google for "duck typing").
> So I have to give up the concept that argument types are part of the
> interface or signature? Honestly, I don't like that. Granted; not having
> strict type checking makes for great flexibility but the price is you
> either write typchecking code or let the error propagate into the
> function or method. I hope type annotations in py3k will allow for
> something like constraints in C# where you can tell the caller right
> away she's doing something wrong.
>

You're Stockholmed to the gills by the crappy and nearly useless type
systems that you've been exposed to. If you're going to expound on the
merits of statically checked type systems, you should first gain some
experience with the ML family of languages, that actually have real
type systems without casts (like Python!), no escape hatches, and
guarantee the static type correctness of all programs.

Once you've done that, you'll hate the limited, verbose and cumbersome
C# type system and you'll be in a better situation to appreciate both
static and dynamic typing.

On a more pragmatic basis, there are only 2 kinds of type errors in Python:
1: The object passed doesn't implement the correct interface, and will
raise an error when called. This will be caught by a unit test.
2: The object passed implements something that looks like the right
interface, but implements it incorrectly. This will be caught by a
unit tests.

Note that both these errors will be caught by behavior exercising unit
tests and do not rely on any sort of "typechecking code" to be
written. Explicit typechecking in Python is done only when you need to
dispatch on type, not because you feel like generating spurious
errors.



More information about the Python-list mailing list