[Python-3000] PEP 3100 Comments

Talin talin at acm.org
Sun May 7 10:23:57 CEST 2006


Thomas Wouters <thomas <at> python.org> writes:

> It may be 'common' for programmers to try and write this kind of
code (accept either a string or an instance, a sequence or a single
object, etc) but it's certainly not a common idiom -- idioms don't suck 


As an example, lets take a look at re.sub:

   sub(pattern, repl, string[, count])

The documentation says that "repl can be a string or a function" and
"If repl is a function, it is called for every non-overlapping occurrence
of pattern. The function takes a single match object argument, and
returns the replacement string."

This is certainly an example where different actions are taken based on the
type of the object. And as for this being bad style - well, if the standard
library can't be used as a model of API design -- especially a module that
has been refactored as many times as re -- then what can be?

Certainly this ability can be abused to create obscure code, but in this
particular case the usage seems reasonable and relatively straight
forward; I would hate to categorically forbid all uses of APIs that
take actions based on object type rather than strict OOP.

(Admittedly this particular case could be done without 'callable', since you
could test for string type, and assume callable otherwise. However, that's
not always going to be the case.)

-- Talin




More information about the Python-3000 mailing list