Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76225

Re: Why does str not have a __radd__ method?

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 <jason.swails@gmail.com>
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; '&gt;&gt;': 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; '&gt;&gt;&gt;': 0.24; 'unicode': 0.24; 'cc:2**0': 0.24; '&gt;': 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> <mailman.12932.1407950830.18130.python-list@python.org> <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 <jason.swails@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type multipart/alternative; boundary=20cf303e9fc891b3580500871229
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12936.1407955459.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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 "<stdin>", line 1, in <module>
> 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 "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> u'this' + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found


All the best,
Jason

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does str not have a __radd__ method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-14 02:00 +1000
  Re: Why does str not have a __radd__ method? Ethan Furman <ethan@stoneleaf.us> - 2014-08-13 10:27 -0700
    Re: Why does str not have a __radd__ method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-14 03:55 +1000
      Re: Why does str not have a __radd__ method? Ethan Furman <ethan@stoneleaf.us> - 2014-08-13 11:22 -0700
      Re: Why does str not have a __radd__ method? Jason Swails <jason.swails@gmail.com> - 2014-08-13 14:36 -0400
        Re: Why does str not have a __radd__ method? wxjmfauth@gmail.com - 2014-08-13 23:24 -0700

csiph-web