A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2013-October/129135.html below:

[Python-Dev] summing integer and class

[Python-Dev] summing integer and classChris Angelico rosuav at gmail.com
Thu Oct 3 17:51:46 CEST 2013
On Thu, Oct 3, 2013 at 11:09 PM, Игорь Васильев <vasilyev_igor at inbox.ru> wrote:
> When we adding class to integer we have both slotv and slotw. x = slotv(v,
> w); -> returns Py_NotImplemented.
> But in this case we should execute x = slotw(v, w); and function should be
> completed in the same way as when we adding integer to class.
>
> Can someone advise please where I mistake.

No need to dig into the CPython source for this, the answer's pretty
simple: 1+a is handled by __radd__ not __add__.

>>> class A():
    def __add__(self, var):
        print("I'm in A class")
        return 5
    def __radd__(self, var):
        print("I'm in A class, too")
        return 6

>>> a=A()
>>> a+1
I'm in A class
5
>>> 1+a
I'm in A class, too
6

You could ask this sort of thing on python-list at python.org rather than
python-dev.

ChrisA
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4