这个变量声明在 python 中是如何工作的?
How does this variable declaration works in python?
i = 0x0800
这里我的理解是0x0800是一个十六进制数,其中'0x'表示十六进制类型,后面的数字'0800'是一个2字节的十六进制数。在将其分配给变量 'i' 时,检查其类型时出现此错误
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
这里我弄清楚 'i' 应该是一个 int 对象。当我尝试这个时我变得更加困惑
>>> print i
2048
“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'>
注意 type = 42
行;我创建了一个新的全局名称 type
并且在内置之前找到了。您还可以使用 Python 2 中的 import __builtin__; __builtin__.type(i)
或 Python 3 中的 import builtins; builtins.type(i)
来访问原始内置的 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'>
0x
符号只是指定整数文字的几种方法之一。您仍在生成一个常规整数,只是 语法 用于定义值的方式在此处有所不同。以下所有符号产生完全相同的整数值:
0x0800 # hexadecimal
0o04000 # octal, Python 2 also accepts 0400
0b100000000000 # binary
2048 # decimal
我会尽快给出我想出的答案....
i = 0x0800 会将十六进制数 (0800) 的 int 等价物分配给 i.
所以如果我们分解成碎片,这看起来像
>>> i
2048
>>>
>>> (pow(16,3) * 0) + ( pow(16,2) * 8 ) + (pow (16,1) * 0 ) + (pow(16,0) * 0)
2048
i = 0x0800
这里我的理解是0x0800是一个十六进制数,其中'0x'表示十六进制类型,后面的数字'0800'是一个2字节的十六进制数。在将其分配给变量 'i' 时,检查其类型时出现此错误
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
这里我弄清楚 'i' 应该是一个 int 对象。当我尝试这个时我变得更加困惑
>>> print i
2048
“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'>
注意 type = 42
行;我创建了一个新的全局名称 type
并且在内置之前找到了。您还可以使用 Python 2 中的 import __builtin__; __builtin__.type(i)
或 Python 3 中的 import builtins; builtins.type(i)
来访问原始内置的 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'>
0x
符号只是指定整数文字的几种方法之一。您仍在生成一个常规整数,只是 语法 用于定义值的方式在此处有所不同。以下所有符号产生完全相同的整数值:
0x0800 # hexadecimal
0o04000 # octal, Python 2 also accepts 0400
0b100000000000 # binary
2048 # decimal
我会尽快给出我想出的答案....
i = 0x0800 会将十六进制数 (0800) 的 int 等价物分配给 i.
所以如果我们分解成碎片,这看起来像
>>> i
2048
>>>
>>> (pow(16,3) * 0) + ( pow(16,2) * 8 ) + (pow (16,1) * 0 ) + (pow(16,0) * 0)
2048