Python isinstance() 方法
Python isinstance()函数如果对象是指定类型则返回 True,如果不匹配则返回 False。
Syntax : isinstance(obj, class)
Parameters :
- obj : The object that need to be checked as a part of class or not.
- class : class/type/tuple of class or type, against which object is needed to be checked.
Returns : True, if object belongs to the given class/type if single class is passed or any of the class/type if tuple of class/type is passed, else returns False. Raises
TypeError: if anything other than mentioned valid class type.
Python isinstance() 示例
示例 1:带有 int 和 list 的Python isinstance
Python3
# Python 3 code to demonstrate
# working of isinstance()
# with native types
# initializing native types
test_int = 5
test_list = [1, 2, 3]
# testing with isinstance
print("Is test_int integer? : " + str(isinstance(test_int, int)))
print("Is test_int string? : " + str(isinstance(test_int, str)))
print("Is test_list integer? : " + str(isinstance(test_list, int)))
print("Is test_list list? : " + str(isinstance(test_list, list)))
# testing with tuple
print("Is test_int integer or list or string? : "
+ str(isinstance(test_int, (list, int))))
Python3
# Python 3 code to demonstrate
# working of isinstance()
# with objects
# declaring classes
class gfg1:
a = 10
# inherited class
class gfg2(gfg1):
string = 'GeeksforGeeks'
# initializing objects
obj1 = gfg1()
obj2 = gfg2()
# checking instances
print("Is obj1 instance of gfg1? : " + str(isinstance(obj1, gfg1)))
print("Is obj2 instance of gfg2? : " + str(isinstance(obj2, gfg2)))
print("Is obj1 instance of gfg2? : " + str(isinstance(obj1, gfg2)))
# check inheritance case
# return true
print("Is obj2 instance of gfg1? : " + str(isinstance(obj2, gfg1)))
Python3
test_list = [1, 2, 3]
print ("Is test_list list? : " + str(isinstance(test_list, list)))
Python3
test_str = "GeeksforGeeks"
print ("Is test_str string? : " + str(isinstance(test_str, str)))
Python3
test_dict = {"apple" : 1, "Ball" : 2 }
print ("Is test_str dictionary? : " + str(isinstance(test_dict, dict)))
Python
class geeks:
course = 'DSA'
def purchase(obj):
return obj.course
geeks.purchase = classmethod(geeks.purchase)
str(isinstance(geeks.purchase(), str ))
输出:
Is test_int integer? : True
Is test_int string? : False
Is test_list integer? : False
Is test_list list? : True
Is test_int integer or list or string? : True
示例 2:演示 isinstance() 与对象的使用
Python3
# Python 3 code to demonstrate
# working of isinstance()
# with objects
# declaring classes
class gfg1:
a = 10
# inherited class
class gfg2(gfg1):
string = 'GeeksforGeeks'
# initializing objects
obj1 = gfg1()
obj2 = gfg2()
# checking instances
print("Is obj1 instance of gfg1? : " + str(isinstance(obj1, gfg1)))
print("Is obj2 instance of gfg2? : " + str(isinstance(obj2, gfg2)))
print("Is obj1 instance of gfg2? : " + str(isinstance(obj1, gfg2)))
# check inheritance case
# return true
print("Is obj2 instance of gfg1? : " + str(isinstance(obj2, gfg1)))
输出:
Is obj1 instance of gfg1? : True
Is obj2 instance of gfg2? : True
Is obj1 instance of gfg2? : False
Is obj2 instance of gfg1? : True
示例 3: Python isinstance 数组
Python3
test_list = [1, 2, 3]
print ("Is test_list list? : " + str(isinstance(test_list, list)))
输出:
Is test_list list? : True
示例 4: Python isinstance字符串
Python3
test_str = "GeeksforGeeks"
print ("Is test_str string? : " + str(isinstance(test_str, str)))
输出:
Is test_str string? : True
示例 4: Python isinstance 字典
Python3
test_dict = {"apple" : 1, "Ball" : 2 }
print ("Is test_str dictionary? : " + str(isinstance(test_dict, dict)))
输出:
Is test_str dictionary? : True
示例 4: Python isinstace 类方法
Python
class geeks:
course = 'DSA'
def purchase(obj):
return obj.course
geeks.purchase = classmethod(geeks.purchase)
str(isinstance(geeks.purchase(), str ))
输出:
True