Why does Python change the value of an integer when there is a 0 in front of it?

前端 未结 4 387
眼角桃花
眼角桃花 2021-01-14 18:14

I implemented a function converting an integer number to its representation as a string intToStr() (code below).

For testing I\'ve passed in some values

4条回答
  •  既然无缘
    2021-01-14 19:08

    As others have said that's because of octal numbers. But I strongly suggest you to change your function to:

    >>> from functools import partial
    >>> force_decimal = partial(int, base=10)
    >>> force_decimal("01")
    1
    >>> force_decimal("0102301")
    102301
    

    This way you will explicitly force the conversion to base 10. And int wont be inferring it for you.

提交回复
热议问题