我在日常工作中经常使用Python IDLE,主要用于简短的脚本和强大而方便的计算器.
我通常必须使用不同的数字基(大多数是十进制,十六进制,二进制和不太频繁的八进制和其他基数.)
我知道,使用int()
,hex()
,bin()
,oct()
是移动从一个基站到另一个前缀的便捷方式整数文字与正确的前缀是另一种方式来表达一个数.
我发现在一个函数中进行计算只是为了在正确的基础上看到结果(并且产生的hex()
和类似函数的输出是一个字符串)非常不方便,所以我想要实现的是要么函数(或可能是一个语句?),它将内部IDLE编号表示设置为已知的基数(2,8,10,16).
示例:
>>> repr_hex() # from now on, all number are considered hexadecimal, in input and in output
>>> 10 # 16 in dec
>>> 0x10 # now output is also in hexadecimal
>>> 1e + 2
>>> 0x20
# override should be possible with integer literal prefixes
# 0x: hex ; 0b: bin ; 0n: dec ; 0o: oct
>>> 0b111 + 10 …
Run Code Online (Sandbox Code Playgroud)