如何确定 Python 变量的类型? [英] How to determine a Python variable's type?

查看:73
本文介绍了如何确定 Python 变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查看变量的类型是无符号 32 位、有符号 16 位等?

如何查看?

解决方案

使用type() 内置函数:

<预><代码>>>>我 = 123>>>类型(i)<输入'int'>>>>type(i) 是 int真的>>>我 = 123.456>>>类型(i)<输入'浮动'>>>>类型(i)是浮点数真的

要检查变量是否属于给定类型,请使用 isinstance:

<预><代码>>>>我 = 123>>>isinstance(i, int)真的>>>isinstance(i, (float, str, set, dict))错误的

请注意,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆