python isinstance()函数和type()函数

一、type()用法

描述:

  python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型。当有三个参数的时候返回一个类对象。

语法:

  一个参数:type(object)

  三个参数:type(name,bases,dict)

用法:

一个参数时,type()返回一个对象的数据类型

 1 >>> type(1)
 2 <class 'int'>
 3 >>> type('alex')
 4 <class 'str'>
 5 >>> type([1,2,3])
 6 <class 'list'>
 7 >>> type((1,2,3))
 8 <class 'tuple'>
 9 >>> type({'zero':0,'one':1})
10 <class 'dict'>
11 >>> type(1) == int
12 True
13 >>>

三个参数时:

name:类名

bases: 父类的元组

dict: 类的属性方法和值组成的键值对

创建一个类

 1 # 构造函数
 2 def __init__(self, name):
 3     self.name = name
 4 # 实例(普通)方法
 5 def instancetest(self):
 6     print('this is instance method')
 7 
 8 # 类方法
 9 @classmethod
10 def classtest(cls):
11     print('This is a class method')
12 
13 # 静态方法
14 @staticmethod
15 def statictest(n):
16     print('This is a static method %s' % n)
17 
18 #创建类
19 test_property = {'number': 1, '__init__':__init__,'instancetest1':instancetest,
20                             'classtest': classtest, 'statictest': statictest}# 属性和方法
21 Test = type('Tom', (object,), test_property)
22 
23 # 实例化
24 test = Test('alex')
25 print(test.name)
26 print(test.number)
27 test.instancetest1()
28 test.classtest()
29 test.statictest(7)

执行结果:

1 alex
2 1
3 this is instance method
4 This is a class method
5 This is a static method 7

 用help()打印Test的详细信息

class Tom(builtins.object)
 |  Tom(name)
 |  
 |  Methods defined here:
 |  
 |  __init__(self, name)
 |      # 构造函数
 |  
 |  instancetest1 = instancetest(self)
 |      # 实例(普通)方法
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  classtest() from builtins.type
 |      # 类方法
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  statictest(n)
 |      # 静态方法
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  number = 1

可以看出我们创建了一个Test类,包含一个实例方法包含一个构造方法__init__,实例方法statictest,类方法classtest,静态方法statictest1,和一个属性number =1。

注意:

Type和Object

type为对象的顶点,所有对象都创建自type。

object为类继承的顶点,所有类都继承自object。

python中万物皆对象,一个python对象可能拥有两个属性,__class__ 和 __base____class__ 表示这个对象是谁创建的,__base__ 表示一个类的父类是谁。

1 >>> object.__class__
2 <class 'type'>
3 >>> type.__base__
4 <class 'object'>

可以得出结论:

  • type类继承自object
  • object的对象创建自type

二、isinstance() 用法

描述:

判断一个对象时否来自一个已知类型

语法:

isinstance(object, classinfo)

参数:

  • object -- 实例对象。
  • classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值:

如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。

1 >>>a = 2
2 >>> isinstance (a,int)
3 True
4 >>> isinstance (a,str)
5 False
6 >>> isinstance (a,(str,int,list))    # 是元组中的一个返回 True
7 True

三、type()和isintance()函数的区别

isinstance() 与 type() 区别:

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

  • isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

1 class A(object):
2     pass
3 class B(A):
4     pass
5 
6 print(isinstance(A(), A))
7 print(isinstance(B(), A))
8 print(type(A()) == A)
9 print(type(B()) == A)

执行结果:

1 True
2 True
3 True
4 False
原文地址:https://www.cnblogs.com/weststar/p/11428312.html