Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76209 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2014-08-14 02:00 +1000 |
| Last post | 2014-08-13 23:24 -0700 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-14 02:00 +1000 |
| Subject | Why does str not have a __radd__ method? |
| Message-ID | <53eb8b8a$0$6574$c3e8da3$5496439d@news.astraweb.com> |
I just tried to override str.__radd__:
class Special(str):
def __radd__(self, other):
print("I'm special!")
return super().__radd__(self, other)
My __radd__ method was called correctly by the + operator, but to my
surprise, the super().__radd__ call failed with:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __radd__
AttributeError: 'super' object has no attribute '__radd__'
Sure enough, in both Python 3.3 and 2.7:
py> str.__radd__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute '__radd__'
This is especially astonishing, since int and float both have __radd__
methods, and yet numeric addition is commutative (x+y == y+x) whereas the
same is not true for string concatenation.
What is the rationale for str not having __radd__ method?
--
Steven
[toc] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-08-13 10:27 -0700 |
| Message-ID | <mailman.12932.1407950830.18130.python-list@python.org> |
| In reply to | #76209 |
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. -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-14 03:55 +1000 |
| Message-ID | <53eba6a3$0$6574$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76219 |
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__' My brain hurts. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-08-13 11:22 -0700 |
| Message-ID | <mailman.12935.1407954172.18130.python-list@python.org> |
| In reply to | #76221 |
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~
[toc] | [prev] | [next] | [standalone]
| From | Jason Swails <jason.swails@gmail.com> |
|---|---|
| Date | 2014-08-13 14:36 -0400 |
| Message-ID | <mailman.12936.1407955459.18130.python-list@python.org> |
| In reply to | #76221 |
[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
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-08-13 23:24 -0700 |
| Message-ID | <655739a7-d606-42c9-aa3d-2e59fcdddc29@googlegroups.com> |
| In reply to | #76225 |
Le mercredi 13 août 2014 20:36:55 UTC+2, Jason Swails a écrit :
> On Wed, Aug 13, 2014 at 1:55 PM, Steven D'Aprano <steve+comp....@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
--------
This is more funny.
>>> print u'a' + 'a'
aa
>>> print 'a' + u'a'
aa
>>> print u'a' + 'é'
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print u'a' + 'é'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
>>>
Python? The single language, which tried (and still tries
to make the ascii world compatible with the unicode
world.
In a somwhow symmetrical way, but a little bit
differently, the situation in Python 3 is even worse.
jmf
PS See my comment about the Euro sign in a previous
thread.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web