最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】Python中,关于isinstance函数的参数的类型有哪些

Python crifan 3339浏览 0评论

isinstance函数的原型是:

isinstance(object, classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct or indirect) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct or indirect) subclass thereof. If object is not a class instance or an object of the given type, the function always returns 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). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.

Changed in version 2.2: Support for a tuple of type information was added.

其中的classinfo指的是某种类型,比如常用的unicode:
isinstance(some_str, unicode)
来判断是否是unicode类型的字符。但是想要知道其他还有哪些类型,网上没找到具体说明,后来还是自己在python安装后,自带的的英文manual中找到了:

5.4. Numeric Types — int, float, long, complex

5.6. Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange

5.7. Set Types — set, frozenset

5.8. Mapping Types — dict

这样,才知道,关于想要判断某个变量的类型,就可以去手册里面找到对应的变量名,然后测试是否是该类型变量了。

转载请注明:在路上 » 【整理】Python中,关于isinstance函数的参数的类型有哪些

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.201 seconds, using 22.06MB memory