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

Ethan Furman ethan at stoneleaf.us
Tue Aug 12 05:03:21 CEST 2014


On 08/11/2014 05:15 PM, Terry Reedy wrote:
> On 8/11/2014 6:23 PM, Wolfgang Maier wrote:
>> On 12.08.2014 00:15, Terry Reedy wrote:
>
>>> Nasty is a subclass of object, with no default value.  Make it a real
>>> str subclass and join works fine.
>>>
>>> class Nasty(str):
>>>      def __radd__(self, other):
>>>          return other + "foo"
>>>
>>> print("".join(["some", "strings", "and", "one", Nasty()]))
>
> If one runs a program with a print statement from an Idle editor,
>
>>>  >>>
>>> somestringsandone
>
> the shell prints the >>> prompt and then the output. I copy the prompt as a separator.
>
>> No, it's not,
>
> I have been copy and pasting python code and output for over 17 years. You could have verified that I did so accurately
> here in less than a minute.  I keep the Idle icon pinned to my task bar and usually have a scratch file for experiments
> in its recent files list, if not already open, to make this sort of thing easy.
>
>> at least not as intended
>
> I was talking about reality, not intentions that were not clear to me.

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.

--
~Ethan~


More information about the Python-ideas mailing list