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() 介绍的更多相关文章

  1. 标准类型内建函数 type()介绍

    我们现在来正式介绍一下 type().在Python2.2 以前, type() 是内建函数.不过从那时起,它变成了一个“工厂函数”.在本章的后面部分我们会讨论工厂函数, 现在你仍然可以将type() ...

  2. python之type函数

    python 的type 函数 的介绍的   下面就是此函数的参数   三个参数的意义 '''type(class_name, base_class_tuple, attribute_dict)cla ...

  3. python 内建函数isinstance的用法以及与type的区别

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  4. python——获取数据类型:type()、isinstance()的使用方法:

    python——获取数据类型   在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' ...

  5. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  6. type与isinstance使用区别

    Python中,type与isinstance都可以用来判断变量的类型,但是type具有一定的适用性,用它来判断变量并不总是能够获取到正确的值. Python在定义变量的时候不用指明具体的的类型,解释 ...

  7. python 内建函数setattr() getattr()

    python 内建函数setattr() getattr() setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为v ...

  8. Python中请使用isinstance()判断变量类型

    一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...

  9. python 内建函数 filter,map和reduce

    python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...

随机推荐

  1. Using sql azure for Elmah

    The MSDN docs contain the list of T-SQL that is either partially supported or not supported.  For ex ...

  2. Cannot install ubuntu or other linux flavours on citrix Xen server

    Citrix Xen sucks! When u try to install linux stuff on its Xen servers, u will get an error complain ...

  3. 设计模式之命令模式(Command)

    #include <iostream> #include <string> using namespace std; class Receiver { public: void ...

  4. [百度空间] [转] 四元数(Quaternions)

    转:四元数(Quaternions) 好吧,我必须承认到目前为止我还没有完全理解四元数,我一度把四元数理解为轴.角表示的4维向量,也就在下午我才从和同事的争辩中理解了四元数不完全是角.轴这么简单,为此 ...

  5. SQLite中的PRAGMA语句攻略

    原文地址:http://iihero.iteye.com/blog/1189633 PRAGMA语句是SQLITE数据的SQL扩展,是它独有的特性,主要用于修改SQLITE库或者内数据查询的操作.它采 ...

  6. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  7. UITbaleView上按钮的单选

    设置Id属性,标记是哪个cell @property (nonatomic,assign)NSInteger Id; 设置一个普通状态和选中状态图片不同的按钮 _choose = [[UIButton ...

  8. UVA 10635 Prince and Princess

    题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...

  9. java 如何连接MySql数据库

    利用jdbc方式连接数据库. 1.添加mysql驱动jar包 我用的是这个驱动包mysql-connector-java-5.1.26-bin.jar 添加方式: 2.加载MySql驱动类 priva ...

  10. js判断一个对象是否包含属性的方式

    判断一个对象是不是包含属性,我们这里提供三种方式 1,使用in 运算符 var obj = {name:'liwei'}; alert('name' in obj); // return true a ...