亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 中的 type() 和 isinstance() 示例

什么是 Python 中的 type()?

Python 有一个名为 type() 的内置函数,可以帮助您找到作为输入的变量的类类型。例如,如果输入是一个字符串,你会得到输出为,对于列表,它将是,等等。

使用 type() 命令,可以传递单个参数,返回值将是给定参数的类类型,例如:type(object)。

也可以将三个参数传递给 type(),即 type(name, bases, dict),在这种情况下,它会返回一个新的类型对象。

在本教程中,您将学习:

type() 的语法:

type() 可以有两种使用方式,如下所示:

 type(object)
type(namr, bases, dict)


参数 :类型(对象)

参数 :type(name, bases, dict)

返回值:

如果对象是传递给 type() 的唯一参数,那么它将返回对象的类型。

如果传递给 type 的参数是 type(object, bases, dict),在这种情况下,它会返回一个新的 object 类型。

type() 示例

在这个例子中,我们有一个字符串值、数字、浮点值、复数、列表、元组、字典和集合。我们将使用带类型的变量来查看每个变量的输出。

str_list = "Welcome to Guru99"
age = 50
pi = 3.14
c_num = 3j+10
my_list = ["A", "B", "C", "D"]
my_tuple = ("A", "B", "C", "D")
my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"}
my_set = {'A', 'B', 'C', 'D'}

print("The type is : ",type(str_list))
print("The type is : ",type(age))
print("The type is : ",type(pi))
print("The type is : ",type(c_num))
print("The type is : ",type(my_list))
print("The type is : ",type(my_tuple))
print("The type is : ",type(my_dict))
print("The type is : ",type(my_set))

输出:

The type is :<class 'str'>
The type is :<class 'int'>
The type is :<class 'float'>
The type is :<class 'complex'>
The type is :<class 'list'>
The type is :<class 'tuple'>
The type is :<class 'dict'>
The type is :<class 'set'>

示例:对类对象使用 type()。

当您使用 type() 检查从类创建的对象时,它会返回类类型以及类的名称。在本例中,我们将创建一个类并检查从类测试中创建的对象类型。

class test:
    s = 'testing'

t = test()
print(type(t))

输出:

<class '__main__.test'>

示例:在 type() 中使用名称、基数和字典

也可以使用以下语法调用类型:type(name, bases, dict)。

传递给 type() 的三个参数,即 name、bases 和 dict 是构成类定义的组件。 name代表类名,bases是基类,dict是基类属性的字典。

在这个例子中,我们将在 type() 中使用所有三个参数,即 name、bases 和 dict。

示例:

class MyClass:
  x = 'Hello World'
  y = 50

t1 = type('NewClass', (MyClass,), dict(x='Hello World', y=50))
print(type(t1))
print(vars(t1))

输出:

<class 'type'>
{'x': 'Hello World', 'y': 50, '__module__': '__main__', '__doc__': None}

当您将所有三个参数传递给 type() 时,它可以帮助您使用基类属性初始化新类。

什么是 Python 中的 isinstance()?

Python isinstance 是 Python 内置函数的一部分。 Python isinstance() 接受两个参数,如果第一个参数是作为第二个参数给出的 classinfo 的实例,则返回 true。

语法 isinstance()

isinstance(object, classtype)

参数

返回值:

如果对象是 classtype 的实例,则返回 true,否则返回 false。

isinstance() 示例

在本节中,我们将研究各种示例来学习 isinstance()

示例:isinstance() 整数校验

下面的代码将整数值 51 与 int 类型进行比较。如果 51 的类型与 int 匹配,则返回 true,否则返回 false。

age = isinstance(51,int)
print("age is an integer:", age)

输出:

age is an integer: True

示例:isinstance() 浮点检查

在此示例中,我们将浮点值与浮点类型进行比较,即 3.14 值将与浮点类型进行比较。

pi = isinstance(3.14,float)
print("pi is a float:", pi)

输出:

pi is a float: True

示例:isinstance() 字符串检查

message = isinstance("Hello World",str)
print("message is a string:", message)

输出:

message is a string: True

示例:isinstance() 元组检查

代码检查类型为元组的元组 (1,2,3,4,5)。如果给定的输入是元组类型,它将返回 true,否则返回 false。

my_tuple = isinstance((1,2,3,4,5),tuple)
print("my_tuple is a tuple:", my_tuple)

输出:

my_tuple is a tuple: True

示例:isinstance() 设置检查

代码检查集合 ({1,2,3,4,5},类型为 set。如果给定的输入是 set 类型,则返回 true,否则返回 false。

my_set = isinstance({1,2,3,4,5},set)
print("my_set is a set:", my_set)

输出:

my_set is a set: True

示例:isinstance() 列表检查

代码检查列表 [1,2,3,4,5],类型为 list。如果给定的输入是 list 类型,则返回 true,否则返回 false。

my_list = isinstance([1,2,3,4,5],list)
print("my_list is a list:", my_list)

输出:

my_list is a list: True

示例:isinstance() 字典检查

代码检查 dict({“A”:”a”, “B”:”b”, “C”:”c”, “D”:”d”}, 类型为 dict。如果给定的输入是 dict 类型,如果不是,则为 false。

my_dict = isinstance({"A":"a", "B":"b", "C":"c", "D":"d"},dict)
print("my_dict is a dict:", my_dict)

输出:

my_dict is a dict: True

示例:类的 isinstance() 测试

该代码显示了使用 isinstance() 的类的类型检查。类的对象与 isinstance() 中的类名进行比较。如果对象属于该类,则返回 true,否则返回 false。

class MyClass:
    _message = "Hello World"

_class = MyClass()

print("_class is a instance of MyClass() : ", isinstance(_class,MyClass))

输出:

_class is a instance of MyClass() True

Python 中 type() 和 isinstance() 的区别

type() isinstance()
Python 有一个名为 type() 的内置函数,可以帮助您找到作为输入的变量的类类型。 Python 有一个名为 isinstance() 的内置函数,该函数将值与给定类型进行比较。如果给定的值和类型匹配,则返回 true,否则返回 false。
返回值是一个类型对象 返回值为布尔值,即真或假。
class A:
my_listA = [1,2,3]

class B(A):
my_listB = [1,2,3]

print(type(A()) == A)
print(type(B()) == A)

输出:

True
False

在类型的情况下,子类检查返回 false。

class A:
my_listA = [1,2,3]

class B(A):
my_listB = [1,2,3]

print(isinstance(A(), A))
print(isinstance(B(), A))

输出:

True
True

isinstance() 在使用子类检查时会给出一个真实的值。

总结:


Python

  1. C# 表达式、语句和块(附示例)
  2. Python 类型转换和类型转换
  3. Python 数字、类型转换和数学
  4. 使用 Raspberry Pi 和 Python 构建机器人
  5. Python Print() 语句:如何通过示例打印
  6. 带有示例的 Python 字符串计数()
  7. Python String format() 举例说明
  8. Python String find() 方法及示例
  9. 带有示例的 Python Lambda 函数
  10. 带有示例的 Python round() 函数
  11. 带有示例的 Python map() 函数
  12. Python Timeit() 与示例