如何确定 Python 变量的类型? [英] How to determine a Python variable's type?
本文介绍了如何确定 Python 变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何查看变量的类型是无符号 32 位、有符号 16 位等?
如何查看?
解决方案
使用type()
内置函数:
要检查变量是否属于给定类型,请使用 isinstance代码>
:
请注意,Python 没有与 C/C++ 相同的类型,这似乎是您的问题.
How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?
How do I view it?
解决方案
Use the type()
builtin function:
>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123.456
>>> type(i)
<type 'float'>
>>> type(i) is float
True
To check if a variable is of a given type, use isinstance
:
>>> i = 123
>>> isinstance(i, int)
True
>>> isinstance(i, (float, str, set, dict))
False
Note that Python doesn't have the same types as C/C++, which appears to be your question.
这篇关于如何确定 Python 变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文