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


Groups > comp.lang.python > #55417

Re: [Python-Dev] summing integer and class

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 <ckaynor@zindagigames.com>
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; '&gt;&gt;&gt;': 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 <ckaynor@zindagigames.com>
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" <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 <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.679.1380816353.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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 <igor.vasilyev@oracle.com>
 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 <module>
>     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

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


Thread

Re: [Python-Dev] summing integer and class Chris Kaynor <ckaynor@zindagigames.com> - 2013-10-03 08:58 -0700

csiph-web