strange math?
Christian Stapfer
nil at dev.nul
Sun Mar 19 00:36:07 EST 2006
<joe.hrbek at gmail.com> wrote in message
news:1142745707.384085.42050 at i39g2000cwa.googlegroups.com...
> Hello everyone, I'm experimenting with python and i'm following this
> tutorial:
> http://docs.python.org/tut/node6.html#SECTION006400000000000000000 I'm
> in section 4.7.5 Lambda Forms. In this section I was working along and
> I noticed something strange. It happened because of a typo. Below is
> a copy/paste from my idle session:
>
>>>>def make_incrementor(n):
> return lambda x: x+n
>
>>>>f=make_incrementor(42)
>>>>f(0)
> 42
>>>>f(1)
> 43
>>>>f(10)
> 52
>>>>f(0)
> 42
>>>>f(01)
> 43
>>>>f(02)
> 44
>>>>f(010)
> 50
>>>>42+010
> 50
>
> The first f(01) was a mistake. I accidentally forgot to delete the
> zero, but to my suprise, it yielded the result I expected. So, I tried
> it again, and viola, the right answer. So, I decided to really try and
> throw it for a loop, f(010), and it produced 50. I expected 52
> (42+10). Why doesn't python ignore the first zero and produce a result
> of 52?
That's because python interprets 010 as *octal* 10
which is *decimal* 8. Thus
42+010 = 42+8 = 50
which is quite as it should be...
Regards,
Christian
More information about the Python-list
mailing list