[Python-ideas] The non-obvious nature of str.join (was Re: sum(...) limitation)

Stephen J. Turnbull stephen at xemacs.org
Tue Aug 12 08:29:20 CEST 2014


Ethan Furman writes:

 > His remark was pointed at the fact that your output is missing the
 > final "foo".  Remove the 'r' from __radd__, though, and you would
 > have what you were trying to demonstrate.

Believe someone when he says he copy-pasted. ;-)  Unless you've
actually run the code and got a different result, and even then you
should probably include version information etc.

The presence of __radd__ (or __add__, for that matter, although it's
reasonably difficult to create a str derivative without it) is
irrelevant to why "".join works when Nasty is derived from str.
You're confusing the actual semantics of str.join (repeated copying at
appropriate offsets into a sufficiently large buffer) with the naive
implementation of sum (an iterated application of '+').



More information about the Python-ideas mailing list