如何避免以"0"开头的python数字文字被视为八进制? [英] how to avoid python numeric literals beginning with "0" being treated as octal?

查看:432
本文介绍了如何避免以"0"开头的python数字文字被视为八进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小的Python 2.x API以支持获取 job by jobNumber,其中jobNumber作为整数提供. 有时用户提供jobNumber作为整数文字 以0开头,例如037537. (这是因为他们一直 由R(一种理智地认为037537==37537的语言)抚慰.) 但是,Python会考虑以"0"开头的整数文字 为OCTAL,因此为037537!=37537,而不是037537==16223.这 使我成为对最小原则的公然侮辱 令人惊讶的是,幸运的是,这看起来像是在Python中修复的 3 ---请参见 PEP 3127 .

I am trying to write a small Python 2.x API to support fetching a job by jobNumber, where jobNumber is provided as an integer. Sometimes the users provide ajobNumber as an integer literal beginning with 0, e.g. 037537. (This is because they have been coddled by R, a language that sanely considers 037537==37537.) Python, however, considers integer literals starting with "0" to be OCTAL, thus 037537!=37537, instead 037537==16223. This strikes me as a blatant affront to the principle of least surprise, and thankfully it looks like this was fixed in Python 3---see PEP 3127.

但是我目前仍停留在Python 2.7上.因此,我的用户可以这样做:

But I'm stuck with Python 2.7 at the moment. So my users do this:

>>> fetchJob(037537)

并默默地得到错误的工作(16223),或者这样:

and silently get the wrong job (16223), or this:

>>> fetchJob(038537)
File "<stdin>", line 1
 fetchJob(038537)
            ^
SyntaxError: invalid token

Python拒绝八进制不兼容的数字.

where Python is rejecting the octal-incompatible digit.

似乎没有任何东西通过__future__提供给 让我了解Py3K的行为-它必须是内置的 以某种方式转换为Python,因为它需要更改词法分析器 至少.

There doesn't seem to be anything provided via __future__ to allow me to get the Py3K behavior---it would have to be built-in to Python in some manner, since it requires a change to the lexer at least.

有人知道我可以如何保护我的用户 在这种情况下做错了工作?目前我能想到的最好的 的目的是更改该API,以便它使用字符串而不是int.

Is anyone aware of how I could protect my users from getting the wrong job in cases like this? At the moment the best I can think of is to change that API so it take a string instead of an int.

推荐答案

目前,我能想到的最好的办法就是更改该API,以便它使用字符串而不是int.

At the moment the best I can think of is to change that API so it take a string instead of an int.

是的,鉴于这种情况,我认为这是一个合理的选择.

Yes, and I think this is a reasonable option given the situation.

另一种选择是确保您的所有职位编号至少包含大于7的一位,以便添加前导零将立即产生错误而不是不正确的结果,但这似乎比使用字符串更大.

Another option would be to make sure that all your job numbers contain at least one digit greater than 7 so that adding the leading zero will give an error immediately instead of an incorrect result, but that seems like a bigger hack than using strings.

最后一个选择可能是教育您的用户.只需五分钟左右的时间就可以说明不添加前导零,如果这样做,会发生什么情况.即使他们由于过时的习惯而忘记或意外添加了零,但如果他们以前曾听说过,则更有可能发现问题.

A final option could be to educate your users. It will only take five minutes or so to explain not to add the leading zero and what can happen if you do. Even if they forget or accidentally add the zero due to old habits, they are more likely to spot the problem if they have heard of it before.

这篇关于如何避免以"0"开头的python数字文字被视为八进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆