python中合法的八进制_如何避免以“ 0”开头的python数字文字被视为八进制?

1586010002-jmsa.png

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.

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

>>> fetchJob(037537)

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

>>> fetchJob(038537)

File "", line 1

fetchJob(038537)

^

SyntaxError: invalid token

where Python is rejecting the octal-incompatible digit.

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.

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.

解决方案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.

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值