Why does str not have a __radd__ method?

Ethan Furman ethan at stoneleaf.us
Wed Aug 13 14:22:50 EDT 2014


On 08/13/2014 10:55 AM, Steven D'Aprano wrote:
> Ethan Furman wrote:
>
>> On 08/13/2014 09:00 AM, Steven D'Aprano wrote:
>>>
>>> What is the rationale for str not having __radd__ method?
>>
>> At a guess I would say because string only knows how to add itself to
>> other strings, so __add__ is sufficient.
>
> # Python 2.7
> py> "Hello" + u"World"
> u'HelloWorld'
> py> unicode.__radd__
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: type object 'unicode' has no attribute '__radd__'

Well, unicode is a string type, right? ;)

And for the proof:

>>> 'hello'.__add__(u'world')
u'helloworld'
>>> u'hello'.__add__('world')
u'helloworld'


> My brain hurts.

An occupational hazard of unicode, surely.

--
~Ethan~



More information about the Python-list mailing list