duck-type-checking?

Joe Strout joe at strout.net
Fri Nov 14 15:28:27 EST 2008


On Nov 14, 2008, at 12:27 PM, pruebauno at latinmail.com wrote:

> What would be actually interesting would be an switch to the python
> interpreter that internally annotated function parameters with how
> they are used in the function and raised an exception as soon as the
> function is called instead of later. Failing earlier rather than
> later.

That would be interesting, but it wouldn't have helped in the case I  
had last week, where the method being called does little more than  
stuff the argument into a container inside the class -- only to blow  
up much later, when that data was accessed in a certain way.

The basic problem was that the data being stored was violating the  
assumptions of the class itself.  Sometimes in the past I've used a  
"check invariants" method on a class with complex data, and call this  
after mutating operations to ensure that all the class invariants are  
still true.  But this class wasn't really that complex; it's just that  
it assumed all the stuff it's being fed were strings (or could be  
treated as strings), and I inadvertently fed it an NLTK.Tree node  
instead (not realizing that a library method I was calling could  
return such a thing sometimes).

So, in this case, the simplest solution was to have the method that  
initially accepts and stores the data check to make sure that data  
satisfies the assumptions of the class.

Best,
- Joe




More information about the Python-list mailing list