implementing descriptors

Terry Reedy tjreedy at udel.edu
Sat Aug 15 03:53:13 CEST 2009


dippim wrote:

> will say that as this particular requirement is imposed on this class
> by the writer, shouldn't it be the writer's responsibility to enforce
> it, especially, when the cost of enforcement is so low?

I would say that it is the writer's responsibility to set the 
requirement and be clear as to what it is and define the contract with 
respect to the requirement. The latter could either be user 
responsibility -- behavior explicitly undefined if violated -- or writer 
responsibility -- with either fix or raise exception upon violation 
detection.

Python's duck-typing style tends to put responsibility on callers. Consider

def minmidmax(seq):
   seq.sort()
   n = len(seq)
   return seq[0], seq[n//2], seq[n-1]

This requires that seq have sane .sort, .__len__, and .__getitem__ 
methods.  Checks could be added, but most Python programs would simply 
say the the input must be an in-place sortable sequence.

Terry Jan Reedy




More information about the Python-list mailing list