python 检查变量类型_Python检查变量类型– 3种方式

python 检查变量类型

In this tutorial we are going to learn how to check variable type in Python. 

在本教程中,我们将学习如何在Python中检查变量类型。

One of the features of Python is it uses Dynamic data type. It means we need not to specify which type of data the variable is going to hold. One time in our code a variable can hold integer later it may hold some string and this will not give us any error. 

Python的功能之一是使用动态数据类型。 这意味着我们无需指定变量将要保存的数据类型。 在我们的代码中,变量一次可以在以后容纳整数,它可以容纳一些字符串,这不会给我们带来任何错误。

Check out the code below for explanation:

查看以下代码以获取解释:

x = 2
print(x)  # x is holding int
 
x = "I am string"
print(x) # x is holding string

Output

输出量

2
I am string

2 我是 弦

So now the need arises to know the data type of any variable which will be useful in many cases, the same operation may have different effects on different data types.

因此,现在有必要知道在许多情况下将有用的任何变量的数据类型,同一操作可能会对不同的数据类型产生不同的影响。

For example + operator is used for addition in case of int and concatenation in case of string.

例如 ,如果是int,则使用 + 运算符进行加法;如果是字符串,则使用 + 运算符进行连接。

x = 16
y = 8
print(x+y) 
 
x = "16"
y = "8"

Output:

输出:

24
168

24 168

So now the important question is how can we know the data type of variable.

所以现在重要的问题是我们如何才能知道变量的数据类型。

Python检查变量类型 (Python Check Variable Type)

Python has provided three ways to do this.

Python提供了三种方法。

  • type()

    类型()

  • isinstance()

    isinstance()

  • __class__

    __类__

使用type() (Using type())

type() will only take one argument which will be the name of the variable and it will give us information about the type of the variable.

type()将仅接受一个参数,该参数将作为变量的名称,并将为我们提供有关变量类型的信息。

x = 16
print(type(x))
 
x = "16"
print(type(x))
 
x = [[1,2],4,6]
print(type(x))
 
x = 16.10
print(type(x))

Output:

输出:

<class ‘int’>
<class ‘str’>
<class ‘list’>
<class ‘float’>

< 类 'INT'> < 类 'STR'> < class'list ' > < 类 'float' >

x = 15L
print(type(x))

This will give output long for Python version before 3.0 and error for later as the distinction between long and int goes away after Python 3.0

由于3.0之前的long和int之间的区别消失了,这将为3.0之前的Python版本提供long的输出,为稍后的版本提供错误

Before Python 3.0:

在Python 3.0之前:

<type ‘long’>

< 输入 'long' >

After Python 3.0:

在Python 3.0之后:

 x = 15L
         ^
SyntaxError: invalid syntax

  x = 15公升 ^ SyntaxError:语法无效

使用isinstance() (Use of isinstance())

Use of isinstance is bit different than type(). Isinstance take argument variable name and a type and if the variable name is same as type then it returns True otherwise it returns False.

isinstance的使用与type()有点不同。 Isinstance接受参数变量名和类型,如果变量名与type相同,则返回True,否则返回False。

x = 16
print(isinstance(x,int))
 
x = "16"
print(isinstance(x,str))
 
x = [[1,2],4,6]
print(isinstance(x,int))
 
x = 16.10
print(isinstance(x,float))

Output:

输出:

True
True
False
True

真正 真正 真正

isinstance can be used when we are going to perform an operation on according to the type of the variable using if conditions.

当我们要使用if条件根据变量的类型执行操作时,可以使用isinstance。

We can also provide a Python tuple as second argument then if the type of the variable is one from the tuple then the method will return True else it will return False.

我们还可以提供一个Python元组作为第二个参数,然后如果变量的类型是元组中的一种,则该方法将返回True,否则将返回False。

x = 16
print(isinstance(x,(int,str,list)))
 
y = "16"
print(isinstance(y,(int,dict,list)))

Output:

输出:

True
False

真正

This can come in handy when we can perform the same operation on different data types. For example we can perform addition on int and similarly on float also so we can use the Python tuple can perform the same operation in one line instead of writing two if else conditions.

当我们可以对不同的数据类型执行相同的操作时,这可能会派上用场。 例如,我们可以在int上执行加法,也可以在float上执行类似操作,因此我们可以使用Python元组在一行中执行相同的操作,而不是在其他情况下编写两个条件。

使用__class__ (Using  __class__)

We can also use __class__ to know the variable type. Variable_name.__class__ will give us the class of that variable, the output here will be similar as if we are using type().

我们还可以使用__class__来了解变量类型。 Variable_name .__ class__将为我们提供该变量的类,此处的输出类似于我们使用type()。

x = 16
print(x.__class__)
 
x = "16"
print(x.__class__)
 
x = [[1,2],4,6]
print(x.__class__)
 
x = 16.10
print(x.__class__)

Output:

输出:

<class ‘int’>
<class ‘str’>
<class ‘list’>
<class ‘float’>

< 类 'INT'> < 类 'STR'> < class'list ' > < 类 'float' >

Although this will give us correct output similar to using type() method, it is not advised to use __class__ as names that start with __ in python are not part of public API so its best if we don’t use them.

尽管这将为我们提供类似于使用type()方法的正确输出,但不建议使用__class__,因为在python中以__开头的名称不是公共API的一部分,因此最好不要使用它们。

Comment down below if you have any queries or know any other way to check variable type in python.

如果您有任何疑问或知道任何其他方法来检查python中的变量类型,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2019/09/python-check-variable-type.html

python 检查变量类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值