restriction on sum: intentional bug?
pavlovevidence at gmail.com
Sat Oct 17 13:06:02 CEST 2009
On Oct 16, 7:41 pm, Alan G Isaac <alan.is... at gmail.com> wrote:
> > Alan G Isaac<alan.isaac<at> gmail.com> writes:
> >> So of course join is better, as originally noted,
> >> but that does not constitute a reason to intentionally
> >> violate duck typing.
> On 10/16/2009 1:03 PM, Benjamin Peterson wrote:
> > As Stephen pointed out, duck typing is not an absolute.
> I do not recall anyone suggesting it was.
> Do you? If so, cite them.
I don't recall anyone saying it either, however I'll go on record as
Duck typing only helps when you don't know what kind of object you're
getting. In practice, there isn't much useful code you can write that
contains the call sum(x), but where you don't know whether x is a list
of strings or a list of numbers. So you usually do know if your list
contains strings, and if you know you have a list of strings you can
just call "".join.
(You might argue that you could write useful code that doesn't know
whether x is a list of strings or a list of lists. True, but you
shouldn't be calling sum() on lists or lists, either.)
What I'm basically saying here is, by shutting out strings from sum,
you don't really lose much in terms of duck typing, because duck
typing wouldn't have been that helpful anyway.
More information about the Python-list