how to avoid python numeric literals beginning with “0” being treated as octal?

这一生的挚爱 提交于 2019-12-01 16:27:34

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.

Perhaps you could take the input as a string, strip leading zeros, then convert back to an int?

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