[Python-Dev] summing integer and class
Chris Angelico
rosuav at gmail.com
Thu Oct 3 17:51:46 CEST 2013
More information about the Python-Dev mailing list
Thu Oct 3 17:51:46 CEST 2013
- Previous message: [Python-Dev] summing integer and class
- Next message: [Python-Dev] PEP 454: Add a new tracemalloc module (final version)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: [Python-Dev] summing integer and class
- Next message: [Python-Dev] PEP 454: Add a new tracemalloc module (final version)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list