python

python中使用什么函数来判断变量的类型

摘星
986
2021-05-28 08:53:06
栏目: 编程语言

python中判断变量类型的方法:在python中可使用type()函数函数来判断变量的类型,当你只有第一个参数时则返回对象的类型,如果带有三个参数则返回新的类型对象。

python中使用什么函数来判断变量的类型

具体分析如下:

type()语法

#一个参数

type(object)

#三个参数

type(name,bases,dict)

参数说明

object:对象

name:类的名称

bases:基类的元组

dict:字典和类内定义的命名空间变量

示例:

# 一个参数实例

>>> type(1)

<type 'int'>

>>> type('runoob')

<type 'str'>

>>> type([2])

<type 'list'>

>>> type({0:'zero'})

<type 'dict'>

>>> x = 1        

>>> type( x ) == int    # 判断类型是否相等

True 

# 三个参数实例

>>> class X(object):

...     a = 1

...

>>> X = type('X', (object,), dict(a=1))  # 产生一个新的类型 X

>>> X

<class '__main__.X'>

0
看了该问题的人还看了