Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'attribute': 0.07; 'string': 0.09; 'operand': 0.09; 'subject:Why': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; '>>': 0.16; '1):': 0.16; 'buffer,': 0.16; 'cc:name:python list': 0.16; 'concatenate': 0.16; 'concatenated': 0.16; 'rationale': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'typeerror:': 0.16; 'unicode,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'unicode': 0.24; 'cc:2**0': 0.24; '>': 0.26; '(see': 0.26; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; '"",': 0.31; '13,': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; '(most': 0.33; 'guess': 0.33; 'knows': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'subject:?': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'jason': 0.38; 'pm,': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'itself': 0.39; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'different': 0.65; 'results': 0.69; 'subject:have': 0.80; 'ethan': 0.84; 'furman': 0.84; 'hood': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=mevKI84mXcCtgSaf12jRNqZzUuPyKKSb/uH75/YtGgI=; b=zlF/4Rz60hIbefGEg3lsvqcpQj0enGEqs0RR6T4rDj5te8mQA0ynY/7YNBf5Bvf/1Z kuSNW7aaElO/Txwv8fNSsUoWIV8uwyNR5b3foiMYtCOdhhto+e2foKzjlmcc31xk0wwv r4x+7dyZ18lV81KpR9VCLX1Rj1DOlMD2E+LEhay3E9wc+ovRSTsyzaiY6YFpobdU1gLN 4t/EiQb8F+t58VK4jhMkFpeM1RDyAnVVXW2t4ZzGcCeZWDw1hI/KCQUFRnIj78d9svu9 aaxvGBvNMC1SYxXhh4puQXSMTt2mNhefOaSDiWXJZHDckeduHsjFUCTDCgOr+SmhSkOL 1QJA== MIME-Version: 1.0 X-Received: by 10.236.133.65 with SMTP id p41mr9628227yhi.73.1407955015675; Wed, 13 Aug 2014 11:36:55 -0700 (PDT) In-Reply-To: <53eba6a3$0$6574$c3e8da3$5496439d@news.astraweb.com> References: <53eb8b8a$0$6574$c3e8da3$5496439d@news.astraweb.com> <53eba6a3$0$6574$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 13 Aug 2014 14:36:55 -0400 Subject: Re: Why does str not have a __radd__ method? From: Jason Swails To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=20cf303e9fc891b3580500871229 Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407955459 news.xs4all.nl 2849 [2001:888:2000:d::a6]:41861 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76225 --20cf303e9fc891b3580500871229 Content-Type: text/plain; charset=UTF-8 On Wed, Aug 13, 2014 at 1:55 PM, Steven D'Aprano < steve+comp.lang.python@pearwood.info> 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 "", line 1, in > AttributeError: type object 'unicode' has no attribute '__radd__' > This happens because the str.__add__ function calls string_concat under the hood (see Objects/stringobject.c) -- there's a unicode check on the other operand that results in the result of PyUnicode_Concat being returned instead of the concatenated str type. This doesn't require that unicode define __radd__. When the left-hand operand is Unicode, PyUnicode_Concat is called directly (which is why the exception message is different for u'this' + 1 and 'this' + 1): >>> 'this' + 1 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> u'this' + 1 Traceback (most recent call last): File "", line 1, in TypeError: coercing to Unicode: need string or buffer, int found All the best, Jason --20cf303e9fc891b3580500871229 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On We= d, Aug 13, 2014 at 1:55 PM, Steven D'Aprano <steve+= comp.lang.python@pearwood.info> wrote:
Ethan Furman wrote:

> On 08/13/2014 09:00 AM, Steven D'Aprano wrote:
>>
>> What is the rationale for str not having __r= add__ 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):
=C2=A0 File "<stdin>", line 1, in <module>
AttributeError: type object 'unicode' has no attribute '_= _radd__'

This happens because the str.__add__ function call= s string_concat under the hood (see Objects/stringobject.c) -- there's = a unicode check on the other operand that results in the result of PyUnicod= e_Concat being returned instead of the concatenated str type. =C2=A0This do= esn't require that unicode define __radd__.

When the left-hand operand i= s Unicode, PyUnicode_Concat is called directly (which is why the exception = message is different for u'this' + 1 and 'this' + 1):

>>> 'this' + 1
Tracebac= k (most recent call last):
=C2=A0 File "<stdin>", line 1,= in <module>
TypeError: cannot conc= atenate 'str' and 'int' objects
>>> u'this' + 1
Tracebac= k (most recent call last):
=C2=A0 File &q= uot;<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found
=

All the best,
Jason
--20cf303e9fc891b3580500871229--