har*_*eyD 3 python hex python-2.7
i = 0x0800
Run Code Online (Sandbox Code Playgroud)
我在这里理解的是0x0800是十六进制数,其中'0x'表示十六进制类型,后面的数字'0800'是一个2字节的十六进制数.在将其分配给变量'i'时,如果检查其类型,则会出现此错误
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)
在这里我得出'i'假设是一个int对象.当我尝试这个时,我变得更加困惑
>>> print i
2048
Run Code Online (Sandbox Code Playgroud)
究竟什么是'2048'..谁能在这里点亮一些光?
i是一个整数,但你重新定义type:
>>> i = 0x0800
>>> i
2048
>>> type(i)
<type 'int'>
>>> type = 42
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(i)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
注意这type = 42条线; 我创建了一个新的全局名称type,它在内置之前找到.您还可以import __builtin__; __builtin__.type(i)在Python 2或import builtins; builtins.type(i)Python 3中使用以访问原始内置type()函数:
>>> import __builtin__
>>> type = 42
>>> __builtin__.type(type)
<type 'int'>
>>> type(type)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(type)
<type 'type'>
Run Code Online (Sandbox Code Playgroud)
该0x符号只是指定一个整数文字的几种方式之一.您仍在生成常规整数,此处仅定义值的语法不同.以下所有表示法都会生成完全相同的整数值:
0x0800 # hexadecimal
0o04000 # octal, Python 2 also accepts 0400
0b100000000000 # binary
2048 # decimal
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |