An Object's Type

paul paul at subsignal.org
Fri Dec 7 11:39:52 EST 2007


Chris Mellon schrieb:
> On Dec 6, 2007 5:52 AM, paul <paul at subsignal.org> wrote:

>> 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.
>>

[language rant snipped]
> 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.
So if I use your code I need to read all of it to understand how "file 
like" something for bla(filelike) needs to be to be able to write 
unittests? Or do your unittests magically detect all possible callers 
and send them email with nice warnings?

> 
> 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.
Do you prefer situations like Hrvoje has descibed two post below?

To reiterate: I'd like to have a TypeError: "foo called with <str>, 
<Point> expected" which is concise and informative for the caller, 
rather than have him hunt down obscure errors in totally unrelated code 
like AttributeError: 'str' object has no attribute 'move'.

greetings
  Paul




More information about the Python-list mailing list