我怎么查看变量的类型?(例如,无符号32位)
我怎么查看变量的类型?(例如,无符号32位)
使用内置函数type()
:
>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123.456
>>> type(i)
<type 'float'>
>>> type(i) is float
True
要检查变量是否为特定类型,请使用isinstance
:
>>> i = 123
>>> isinstance(i, int)
True
>>> isinstance(i, (float, str, set, dict))
False
请注意,Python没有与C/C++相同的类型,这似乎是您的问题所在。
bool
是int
的子类,因此某些令人惊讶的事情是正确的,例如0 == False
和{1,True} == {True}
。 - Quelkleftype()
这个内置函数。>>> v = 10
>>> type(v)
<type 'int'>
>>> v = 100000000000000
>>> type(v)
<type 'long'>
>>> v = -10
>>> type(v)
<type 'int'>
字面字符序列:
>>> v = 'hi'
>>> type(v)
<type 'str'>
>>> v = 3.14159
>>> type(v)
<type 'float'>
这很简单。你可以像这样做。
print(type(variable_name))
如何在Python中确定变量的类型?
如果你有一个变量,例如:
one = 1
想知道它的类型吗?
在Python中,几乎所有的事情都有正确的和错误的做法。以下是正确的方法:
type
>>> type(one)
<type 'int'>
你可以使用__name__
属性获取对象的名称。(这是你需要使用__dunder__
名称才能访问的少数特殊属性之一 - 即使在inspect
模块中也没有相应的方法。)
>>> type(one).__name__
'int'
__class__
在Python中,以下划线开头的名称从语义上讲不是公共API的一部分,用户最好避免使用它们。(除非绝对必要。)
由于type
提供了对象的类,我们应该避免直接获取它。:
>>> one.__class__
当在一个方法中获取对象的类型时,人们通常会想到查找属性,这种情况下使用 type 似乎有些奇怪。例如:
class Foo(object):
def foo(self):
self.__class__
不要这样做。相反,使用type(self)。
class Foo(object):
def foo(self):
type(self)
如何查看变量的类型,例如无符号32位,带符号16位等?
在Python中,这些细节是实现细节。因此,通常我们不必在Python中担心这个问题。但是,如果你好奇的话...
在Python 2中,int通常是一个有符号整数,等于实现的字长(受系统限制)。它通常在C语言中实现为long。当整数超过这个范围时,我们通常将它们转换为Python longs(具有无限精度,不要与C longs混淆)。
例如,在32位Python 2中,我们可以推断出int是一个带符号的32位整数:
>>> import sys
>>> format(sys.maxint, '032b')
'01111111111111111111111111111111'
>>> format(-sys.maxint - 1, '032b') # minimum value, see docs.
'-10000000000000000000000000000000'
在Python 3中,旧的int类型被废弃了,我们现在使用(Python的)long作为int类型,它具有无限精度。
我们还可以了解一些关于Python浮点数的信息,它们通常在C语言中实现为double:
>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)
不要使用语义上非公共的 API __class__
来获取变量的类型,应该使用 type
。
不必过于担心 Python 的实现细节。我自己没有遇到过有关此问题的问题。你可能也不会,如果你确实遇到了,你应该知道不要寻求这个答案来解决问题。
print type(variable_name)
我强烈推荐在处理类似问题时使用 IPython 交互式解释器。它允许您键入 variable_name?
并返回有关该对象的整个信息列表,包括类型和类型的文档字符串。例如:
In [9]: var = 123
In [10]: var?
Type: int
Base Class: <type 'int'>
String Form: 123
Namespace: Interactive
Docstring:
int(x[, base]) -> integer
将字符串或数字转换为整数,如果可能的话。浮点参数将向零舍入(这不包括浮点数的字符串表示!)在转换字符串时,使用可选的基数。在转换非字符串时提供基数是错误的。如果参数超出整数范围,将返回一个长整型对象。
print type(str)
在Python 3.6中会返回错误。请改为使用 type(str)
。 - Kellen Stuartprint(type(str))
。 - jcoppensprint type(var)
错误的代码。 - Han XIAOa = "cool"
type(a)
//result 'str'
<class 'str'>
or
do
`dir(a)`
to see the list of inbuilt methods you can have on the variable.
Python中简单类型检查的示例:
assert type(variable_name) == int
assert type(variable_name) == bool
assert type(variable_name) == list
type()
函数的结果,我们希望它可以进行类型检查,但如何实现并不明显(我假设您需要将类型作为字符串进行比较:if (type(var)=='str'):
)。 - Synetech还有一种方法是使用__class__
:
>>> a = [1, 2, 3, 4]
>>> a.__class__
<type 'list'>
>>> b = {'key1': 'val1'}
>>> b.__class__
<type 'dict'>
>>> c = 12
>>> c.__class__
<type 'int'>
这可能有点不相关,但你可以使用isinstance(object, type)
来检查对象的类型,如此处所述。
这个问题有些含糊不清 -- 我不确定你所说的“view”是什么意思。如果你想要查询一个原生的Python对象的类型,@atzz的回答会指引你正确的方向。
但是,如果你想要生成具有C语言原始类型语义的Python对象(例如uint32_t
、int16_t
等),可以使用struct
模块。你可以通过以下方法确定给定C类型原型中的位数:
>>> struct.calcsize('c') # char
1
>>> struct.calcsize('h') # short
2
>>> struct.calcsize('i') # int
4
>>> struct.calcsize('l') # long
4
这也反映在array
模块中,该模块可以创建这些低级类型的数组:
>>> array.array('c').itemsize # char
1
Python 2所支持的最大整数(即int
)由sys.maxint给出。
>>> import sys, math
>>> math.ceil(math.log(sys.maxint, 2)) + 1 # Signedness
32.0
还有sys.getsizeof,它返回在剩余内存中Python对象的实际大小:
>>> a = 5
>>> sys.getsizeof(a) # Residual memory.
12
对于浮点数据和精度数据,请使用sys.float_info:
>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)