[Tutor] Calling a number's methods

Mark Young marky1991 at gmail.com
Wed Jun 23 07:51:04 CEST 2010


Why does this work

>>> a = 6
>>> b = 7
>>> answer = a.__sub__(b.__neg__())
>>> answer
13


but this does not?

>>> answer = 6.__sub__(7.__neg__())
SyntaxError: invalid syntax

I searched the internet, and found someone suggest adding spaces after each
number, which indeed works properly.

>>> answer = 6 .__sub__(7 .__neg__())
>>> answer
13

Why does this work? I don't even understand why python recognizes that I'm
trying to access the numbers' __sub__ and __neg__ methods, I would think
that the spaces would cause it to not work, but obviously it works just
fine.

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100623/857a4f34/attachment.html>


More information about the Tutor mailing list