python 内建函数 type() 和 isinstance() 介绍
Python 不支持方法或函数重载, 因此你必须自己保证调用的就是你想要的函数或对象。一个名字里究竟保存的是什么?相当多,尤其是这是一个类型的名字时。确认接收到的类型对象的身份有很多时候都是很有用的。为了达到此目的,Python 提供了一个内建函数type(). type()返回任意Python 对象对象的类型,而不局限于标准类型。让我们通过交互式解释器来看几个使用type()内建函数返回多种对象类型的例子:
>>> type('')
<type 'str'>
>>>
>>> s = 'xyz'
>>> type(s)
<type 'str'>
>>>
>>> type(100)
<type 'int'>
>>> type(0+0j)
<type 'complex'>
>>> type(0L)
<type 'long'>
>>> type(0.0)
<type 'float'>
>>>
>>> type([])
<type 'list'>
>>> type(())
<type 'tuple'>
>>> type({})
<type 'dict'>
>>> type(type)
<type 'type'>
>>>
>>> class Foo: pass # new-style class
...
>>> foo = Foo()
>>> class Bar(object): pass # new-style class
...
>>> bar = Bar()
>>>
>>> type(Foo)
<type 'classobj'>
>>> type(foo)
<type 'instance'>
>>> type(Bar)
<type 'type'>
>>> type(bar)
<class '__main__.Bar'>
Python2.2 统一了类型和类, 如果你使用的是低于Python2.2 的解释器,你可能看到不一样的输出结果。
>>> type('')
<type 'string'>
>>> type(0L)
<type 'long int'>
>>> type({})
<type 'dictionary'>
>>> type(type)
<type 'builtin_function_or_method'>
>>>
>>> type(Foo) # assumes Foo created as in above
<type 'class'>
>>> type(foo) # assumes foo instantiated also
<type 'instance'>
python 内建函数 type() 和 isinstance() 介绍的更多相关文章
- 标准类型内建函数 type()介绍
我们现在来正式介绍一下 type().在Python2.2 以前, type() 是内建函数.不过从那时起,它变成了一个“工厂函数”.在本章的后面部分我们会讨论工厂函数, 现在你仍然可以将type() ...
- python之type函数
python 的type 函数 的介绍的 下面就是此函数的参数 三个参数的意义 '''type(class_name, base_class_tuple, attribute_dict)cla ...
- python 内建函数isinstance的用法以及与type的区别
isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是classinfo的实例,或者object是class ...
- python——获取数据类型:type()、isinstance()的使用方法:
python——获取数据类型 在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- type与isinstance使用区别
Python中,type与isinstance都可以用来判断变量的类型,但是type具有一定的适用性,用它来判断变量并不总是能够获取到正确的值. Python在定义变量的时候不用指明具体的的类型,解释 ...
- python 内建函数setattr() getattr()
python 内建函数setattr() getattr() setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为v ...
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- python 内建函数 filter,map和reduce
python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...
随机推荐
- 读书笔记:<我是一只IT小小鸟>
<我是一只IT小小鸟>第一次听到这本书的时候,我便有了深深的好奇,虽然我是一名学习软件工程的大学生,但是还是第一次听到“IT”这个名词,既陌生又好奇.听到老师提起了这本书的意义以及看法,我 ...
- Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块
题目链接: http://codeforces.com/contest/103/problem/D D. Time to Raid Cowavans time limit per test:4 sec ...
- 【BZOJ】【2084】【POI2010】Antisymmetry
Manacher算法 啊……Manacher修改一下就好啦~蛮水的…… Manacher原本是找首尾相同的子串,即回文串,我们这里是要找对应位置不同的“反回文串”(反对称?233) 长度为奇数的肯定不 ...
- Zend Studio 错误集锦[PHP]
错误信息:Cannot create linked resource '/.org.eclipse.dltk.core.external.folders/.link6'. The parent re ...
- draw call 的优化
用一张贴图,renderstate都设置成一致,多个draw合并成一个
- HDU-2604_Queuing
题目:Problem Description Queues and Priority Queues are data structures which are known to most comput ...
- C#正则表达式大全{转}
只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n}$". 只能输入至少n位的数字:"^\d{n,}$". 只能输入m~ ...
- 安装wine qq2012
添加软件源:vi /etc/apt/sources.list deb http://http.kali.org/kali kali main non-free contribdeb-src http: ...
- iOS工程预编译文件的创建
在搜索 添加工程名/自己的pch文件名记住加后缀
- Win7系统配置IIS7服务
1.开启IIS7服务 打开控制面板,选择并进入“程序”,双击“打开或关闭Windows服务”,在弹出的窗口中选择“Internet信息服务”下面所有地选项,点击确定后,开始更新服务. 2.安装web文 ...