restriction on sum: intentional bug?

Tim Chase python.list at tim.thechases.com
Mon Oct 19 06:24:01 EDT 2009


Carl Banks wrote:
> Consider this thought experiment:
> 
> class Something(object):
>     def __radd__(self,other):
>         return other + "q"
> 
> x = ["a","b","c",Something()]
> 
> If x were passed to "".join(), it would throw an exception; but if
> passed to a sum() without any special casing, it would successfully
> return "abcq".

Okay...this is the best argument I've heard for not using 
"".join()  {Awards Carl one (1) internet}  It's a peculiar thing 
to do as a programmer, but "".join() certainly produces an 
unexpected behavior which I'd say is worse.  And a lot of this 
discussion has revolved around letting programmers do peculiar 
things if they want.

So as of Carl's example, I'm now pretty solidly in the "Stop 
throwing an exception, just sum the parts even if it's 
inefficient" camp and no longer straddling between that and the 
"".join() camp.  But I'm definitely still not in the "throwing 
exceptions is a good thing" camp.

-tkc





More information about the Python-list mailing list