Odd behaviour of Python tokenizer when parsing integer numbers

隐身守侯 提交于 2019-12-02 05:35:55

Python 3 standardized how all integer literals (other than base 10) were defined: 0?dddd..., where ? is a single letter indicating the base, and each d is interpreted as a digit in the appropriate base. 0... was retained as an exception, since 0 is 0 in any base, and was accepted as such before explicit base specifiers were required.

The biggest change related to this is that a number with a leading zero but no explicit base specifier is no longer assumed to be an octal number. Python 2 accepts both 042 and 0o42 as octal representations of decimal 34. (Early in Python's history, there were only three valid literals, with hexadecimal being the only one with a specifier. 0o... and 0b... were both later additions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!