[英]isinstance(object,type) in python
I'm just going through some python help documents and came across the following piece of code : 我只是浏览了一些python帮助文档,并遇到了以下代码:
isinstance(object, type)
Can anyone explain what does type
mean in the above statement? 谁能解释一下上述陈述中的type
意味着什么?
Thanks, 谢谢,
Vineel Vineel
type
must be an object denoting a type/class, such as int
or str
. type
必须是表示类型/类的对象,例如int
或str
。 Eg, isinstance(1, int)
evaluates to True
, while isinstance(sys.stdin, str)
evaluates to False
. 例如, isinstance(1, int)
计算结果为True
,而isinstance(sys.stdin, str)
计算结果为False
。 If you've defined a class Foo
, then Foo
is also a type object. 如果你已经定义了一个class Foo
,那么Foo
也是一个类型对象。
Edit : as @delnan notes, type
itself is also a type in Python, so isinstance(str, type)
is true because str
is a type, while isinstance('foo', type)
is false. 编辑 :正如@delnan所说, type
本身也是Python中的一个类型,因此isinstance(str, type)
为true,因为str
是一个类型,而isinstance('foo', type)
是false。 object
is also a type in Python, and is the root of the type hierarchy. object
也是Python中的一个类型,是类型层次结构的根。
isinstance(object, classinfo)
object
- object to be checked object
- 要检查的对象
classinfo
- class, type, or tuple of classes and types classinfo
- 类和类型的类,类型或元组
The isinstance()
returns: isinstance()
返回:
True
if the object is an instance or subclass of a class, or any element of the tuple 如果对象是类的实例或子类,或元组的任何元素,则为True
False
otherwise False
否则
eg: a = 1 + 2j
例如: a = 1 + 2j
print(isinstance(1+2j, complex))
output : True
This statement checks if object
has the type of type
. 此语句检查object
是否具有类型type
。 The type
variable should be a class. type
变量应该是一个类。
isinstance(object, classinfo)
Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. 如果object参数是classinfo参数的实例,或者是(直接,间接或虚拟)子类的实例,则返回true。 Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. 如果classinfo是类型对象(新样式类)并且object是该类型的对象或其(直接,间接或虚拟)子类,则也返回true。 If object is not a class instance or an object of the given type, the function always returns false. 如果object不是类实例或给定类型的对象,则该函数始终返回false。 If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). 如果classinfo既不是类对象也不是类型对象,它可能是类或类型对象的元组,或者可能递归地包含其他这样的元组(不接受其他序列类型)。 If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised. 如果classinfo不是类,类型和类别等的类,类型或元组,则会引发TypeError异常。
Changed in version 2.2: Support for a tuple of type information was added. 版本2.2中已更改:添加了对类型信息元组的支持。
Quoted from: http://docs.python.org/library/functions.html#isinstance 引用自: http : //docs.python.org/library/functions.html#isinstance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.