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

刺骨的言语ヽ痛彻心扉 2022-12-07 12:58 494阅读 0赞

ec2d0bd2d4bc4df0838b036577f72141.png

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:

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

  1. x = 2
  2. print(x) # x is holding int
  3. x = "I am string"
  4. 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,则使用 + 运算符进行加法;如果是字符串,则使用 + 运算符进行连接。

  1. x = 16
  2. y = 8
  3. print(x+y)
  4. x = "16"
  5. 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()将仅接受一个参数,该参数将作为变量的名称,并将为我们提供有关变量类型的信息。

  1. x = 16
  2. print(type(x))
  3. x = "16"
  4. print(type(x))
  5. x = [[1,2],4,6]
  6. print(type(x))
  7. x = 16.10
  8. print(type(x))

Output:

输出:

*

*

< 类 ‘INT’> < 类 ‘STR’> < class’list ‘ > < 类 ‘float’ >

  1. x = 15L
  2. 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之前:

< 输入 ‘long’ >

After Python 3.0:

在Python 3.0之后:

  • x = 15L

    1. ^

    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。

  1. x = 16
  2. print(isinstance(x,int))
  3. x = "16"
  4. print(isinstance(x,str))
  5. x = [[1,2],4,6]
  6. print(isinstance(x,int))
  7. x = 16.10
  8. 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。

  1. x = 16
  2. print(isinstance(x,(int,str,list)))
  3. y = "16"
  4. 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()。

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

Output:

输出:

*

*

< 类 ‘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 检查变量类型

发表评论

表情:
评论列表 (有 0 条评论,494人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python 变量类型

    Python 变量类型 变量是存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间。 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存

    相关 Python 变量类型

    Python 变量类型 变量是存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间。 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存

    相关 Python||Python变量类型

    Python 变量类型 变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中