python的types模块

1.types是什么:

  • types模块中包含python中各种常见的数据类型,如IntType(整型),FloatType(浮点型)等等。
>>> import types

>>> dir(types)
['BooleanType',
'BufferType',
'BuiltinFunctionType',
'BuiltinMethodType',
'ClassType',
'CodeType',
'ComplexType',
'DictProxyType',
'DictType',
'DictionaryType',
'EllipsisType',
'FileType',
'FloatType',
'FrameType',
'FunctionType',
'GeneratorType',
'GetSetDescriptorType',
'InstanceType',
'IntType',
'LambdaType',
'ListType',
'LongType',
'MemberDescriptorType',
'MethodType',
'ModuleType',
'NoneType',
'NotImplementedType',
'ObjectType',
'SliceType',
'StringType',
'StringTypes',
'TracebackType',
'TupleType',
'TypeType',
'UnboundMethodType',
'UnicodeType',
'XRangeType',
'__all__',
'__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__']

2.types常见用法:

# 100是整型吗?
>>> isinstance(100, types.IntType)
True >>>type(100)
int # 看下types的源码就会发现types.IntType就是int
>>> types.IntType is int
True
  • 但有些类型并不是int这样简单的数据类型:

class Foo:
def run(self):
return None def bark(self):
print('barking') a = Foo() print(type(1))
print(type(Foo))
print(type(Foo.run))
print(type(Foo().run))
print(type(bark))

输出结果:

<class 'int'>
<class 'type'>
<class 'function'>
<class 'method'>
<class 'function'>
  • python中总有些奇奇怪怪的类型。有些类型默认python中没有像int那样直接就有,单但其实也可以自己定义的。
>>> import types

>>> class Foo:
def run(self):
return None def bark(self):
print('barking') # Foo.run是函数吗?
>>> isinstance(Foo.run, types.FunctionType)
True # Foo().run是方法吗?
>>> isinstance(Foo().run, types.MethodType)
True # 其实:
>>> types.FunctionType is type(Foo.run)
True >>> types.MethodType is type(Foo().run)
True
  • 瞬间感觉types模块号low,直接用type都能代替。。事实就是这样

3.MethodType动态的给对象添加实例方法:

import types
class Foo:
def run(self):
return None def bark(self):
print('i am barking') a = Foo()
a.bark = types.MethodType(bark, a)
a.bark()
  • 如果不用MethodType而是直接a.bark = bark的话,需要在调用bark时额外传递self参数,这不是我们想要的。

python的types模块的更多相关文章

  1. python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性

    python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性 inspect import inspect def fun(): pass inspect.ism ...

  2. python types模块

    types模块成员: ['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'C ...

  3. python标准库介绍——13 types 模块详解

    == types 模块== ``types`` 模块包含了标准解释器定义的所有类型的类型对象, 如 [Example 1-86 #eg-1-86] 所示. 同一类型的所有对象共享一个类型对象. 你可以 ...

  4. Python 利用pytesser模块识别图像文字

    使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the ...

  5. Python学习——struct模块的pack、unpack示例

    he struct module includes functions for converting between strings of bytes and native Python data t ...

  6. 周末班:Python基础之模块

    什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  7. 基于Python的datetime模块和time模块源码阅读分析

    目录 1 前言  2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...

  8. python的库有多少个?python有多少个模块?

    这里列举了大概500个左右的库: !   Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...

  9. python之platform模块

    python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...

随机推荐

  1. mac pro电脑怎么安装rabbitmq

    第一:依次执行以下命令: 1.  /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/inst ...

  2. 解决tomcat占用8080端口问题图文详解

    相信很多朋友都遇到过这样的问题吧,tomcat死机了,重启eclipse之后,发现 Several ports (8080, 8009) required by Tomcat v6.0 Server ...

  3. [Wc2007]剪刀石头布

    [Wc2007]剪刀石头布 http://www.lydsy.com/JudgeOnline/problem.php?id=2597 Time Limit: 20 Sec  Memory Limit: ...

  4. 扫描线(线段树)+贪心 ZOJ 3953

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second       ...

  5. LightOJ 1239 - Convex Fence 凸包周长

    LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...

  6. 「七天自制PHP框架」应用:JSON生成器

    刚刚开始学做一个WebAPP,数据查询的一般套路是通过一张PHP页面读取数据库,获得列表后“嵌写”在PHP页面中,虽然写法上丑陋至极,但也有“快糙猛”出效果的成就感,如图. 后来想想,不对啊,难道以后 ...

  7. JQuery 中三十一种选择器的应用

    选择器(selector)是CSS中很重要的概念,所有HTML语言中的标记都是通过不同的CSS选择器进行控制的.用户只需要通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...

  8. Oracle数据库语句

    Oracle数据库语句 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CHEC ...

  9. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

  10. TinyOS在ubuntu 14.04下安装教程

    1:打开/etc/apt/sources.list 文件,在文件最底部添加安装源: deb http://tinyos.stanford.edu/tinyos/dists/ubuntu lucid m ...