Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'subject:: [': 0.04; 'subject:Python': 0.06; 'modify': 0.07; 'reason,': 0.07; 'function,': 0.09; 'function:': 0.09; 'happens.': 0.09; 'operand': 0.09; 'operator,': 0.09; 'override': 0.09; 'python': 0.11; 'def': 0.12; '"a"': 0.16; '(it': 0.16; 'a()': 0.16; 'a():': 0.16; 'fails.': 0.16; 'igor': 0.16; 'operator.': 0.16; 'subject:Dev': 0.16; 'subject:class': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'else,': 0.19; 'thu,': 0.19; 'subject:] ': 0.20; '>>>': 0.22; 'example': 0.22; 'to:name:python- list@python.org': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'integer': 0.24; 'received:209.85.216.46': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'define': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'fine,': 0.31; 'provided,': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'entry': 0.36; 'method': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'received:209.85.216': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'simple': 0.61; 'sample': 0.67; 'below.': 0.71; 'overloading': 0.84; 'type(s)': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=waE4mr4Dl8ThoHrIpXB72Vy5xTIDIGPjbzwLi/1Gi3o=; b=TccgFd0kczQnqRaf1c2x3oVMwqX8ekXOQWZRt0ycPmSpFiWjBSy68+oeLqCgQFeBDe CEmkTcBcpRinZFd550Gt4BoW3ykXLyB7bIsmbzSov56USXPg0XC20YInxEGJxjFvueku XsEePtXHVeyl0zRoFpXdVeD5Gk4FleTWR7Dtfsd5hRu85ns9YMPB0qis3HdXW7MmZwrL P0lBKh+/rydw7YF/DSPkpfgKmhgtN4IKULgkuZcrE30z76H+/2cIw8BuJ9TtFchDscMR S3RMCEdClH9UltXaE9fTbHrnUl5nXFDDE9rvmI7RK8JtZXQhTVq8JIYZLvbzQgmTOkFT i8tQ== X-Gm-Message-State: ALoCoQkibx8U9HWQnRU/FoIFa2zsGzeCuAcAFhCbKts9yhfb6L2s7BOQ/MON3eUfEwYUWeqWaK4m X-Received: by 10.224.76.10 with SMTP id a10mr11664775qak.9.1380815944246; Thu, 03 Oct 2013 08:59:04 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <524D74EE.90308@oracle.com> References: <524D74EE.90308@oracle.com> From: Chris Kaynor Date: Thu, 3 Oct 2013 08:58:44 -0700 Subject: Re: [Python-Dev] summing integer and class To: igor.vasilyev@oracle.com, "python-list@python.org" Content-Type: multipart/alternative; boundary=001a11c305a4db763304e7d843e4 Cc: python-dev@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380816353 news.xs4all.nl 15991 [2001:888:2000:d::a6]:53052 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55417 --001a11c305a4db763304e7d843e4 Content-Type: text/plain; charset=ISO-8859-1 This list is for development OF Python, not for development in python. For that reason, I will redirect this to python-list as well. My actual answer is below. On Thu, Oct 3, 2013 at 6:45 AM, Igor Vasilyev wrote: > Hi. > > Example test.py: > > class A(): > def __add__(self, var): > print("I'm in A class") > return 5 > a = A() > a+1 > 1+a > Execution: > python test.py > I'm in A class > Traceback (most recent call last): > File "../../test.py", line 7, in > 1+a > TypeError: unsupported operand type(s) for +: 'int' and 'instance' > > > So adding integer to class works fine, but adding class to integer fails. > I could not understand why it happens. In objects/abstact.c we have the > following function: > Based on the code you provided, you are only overloading the __add__ operator, which is only called when an "A" is added to something else, not when something is added to an "A". You can also override the __radd__ method to perform the swapped addition. See http://docs.python.org/2/reference/datamodel.html#object.__radd__ for the documentation (it is just below the entry on __add__). Note that for many simple cases, you could define just a single function, which then is defined as both the __add__ and __radd__ operator. For example, you could modify your "A" sample class to look like: class A(): def __add__(self, var): print("I'm in A") return 5 __radd__ = __add__ Which will produce: >>> a = A() >>> a + 1 I'm in A 5 >>> 1 + a I'm in A 5 Chris --001a11c305a4db763304e7d843e4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
This list is for development OF= Python, not for development in python. For that reason, I will redirect th= is to python-list as well. My actual answer is below.

On Thu, Oct 3, 2013 at 6:45 AM, Igor Vasilye= v=A0<igor.vasilyev@oracle.com>=A0wrote:
Hi.

Example test.py:

class A():
=A0 =A0 def __add__(self, = var):
=A0 =A0 =A0 =A0 print("I'm in A class")
=A0 =A0 = =A0 =A0 return 5
a =3D A()
a+1
1+a=A0

Execution:
python test.py
I'm in A class
Traceback (most r= ecent call last):
=A0 File "../../test.py", line 7, in <mod= ule>
=A0 =A0 1+a
TypeError: unsupported operand type(s) for +: = 9;int' and 'instance'


So adding integer to class works fine, but adding class to integer = fails.
I could not understand why it happens. In objects/abstact.c we ha= ve the following function:

Based on the= code you provided, you are only overloading the __add__ operator, which is= only called when an "A" is added to something else, not when som= ething is added to an "A". You can also override the __radd__ met= hod to perform the swapped addition. See=A0http://docs.python.org/2/refe= rence/datamodel.html#object.__radd__=A0for the documentation (it is jus= t below the entry on __add__).

Note that for many simple cases, you could define just = a single function, which then is defined as both the __add__ and __radd__ o= perator. For example, you could modify your "A" sample class to l= ook like:

class A():
=A0 =A0 def __add__(self, var):
=A0 =A0 =A0 =A0 print("I'm in A")
=A0 =A0 = =A0 =A0 return 5
=A0 =A0 __radd__ =3D __add__


Which will produce:
>>> a =3D A()
>>> a + 1
I&#= 39;m in A
5
>>> 1 + a
I'm in A
5

Chris
--001a11c305a4db763304e7d843e4--