[Chicago] duck typing to handle a string or iterable of strings

Martin Maney maney at two14.net
Thu May 19 19:14:37 CEST 2011


On Thu, May 19, 2011 at 10:27:04AM -0500, Jeremy McMillan wrote:
> So I want a function or method to operate on one or more strings, ...

My first thought was what Carl suggested: foo(*args).  But if the
problem is you have too many semi-competent programmers and must
accomodate their lack of attention to detail I suppose this won't
appeal to you either.  And I'll bet it's much too late to give them a
simpler interface such as  foo(one_string) and foo_many(list_of_strings).

> The whole point of duck-typing is to be as lenient as possible with
> subclasses and workalike objects. Theoretically, it would work pretty
> well to use any of those 41 methods and attributes, but which one is
> optimal depends on convention.

Nope.  Duck typing means you only care about the methods or attributes
that you need; examining the type, directly or by sniffing out other
methods as you seem to want to do, is not ducky.  ;-/

Duck-typing means you test, very possibly by just doing it, exactly the
methods or attributes you need.  Pre-testing even for one or more of
these is rare; the Pythonic idiom is that it's easier to seek
forgiveness (catch an exception) than get permission (viz., if
getattr(x, 'foo', None): bar(x.foo) ).

-- 
The vulnerabilities will come, a couple of them a week, for years and
years...until people stop looking for them.  Waiting six months isn't
going to make this OS [Windows XP] safer.  -- Bruce Schneier



More information about the Chicago mailing list