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. AI技术说:人工智能相关概念与发展简史

    作为近几年的一大热词,人工智能一直是科技圈不可忽视的一大风口.随着智能硬件的迭代,智能家居产品逐步走进千家万户,语音识别.图像识别等AI相关技术也经历了阶梯式发展.如何看待人工智能的本质?人工智能的飞 ...

  2. 前端PHP入门-016-静态变量

    如果我想知道函数被调用了多少次怎么办?在没有学习静态变量的时候,我们没有好的办法来解决. 静态变量的特点是:声明一个静态变量,第二次调用函数的时候,静态变量不会再初始化变量,会在原值的基础上读取执行. ...

  3. Tensorflow BatchNormalization详解:4_使用tf.nn.batch_normalization函数实现Batch Normalization操作

    使用tf.nn.batch_normalization函数实现Batch Normalization操作 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴恩达deeplearnin ...

  4. Java的StringAPI的小练习

    //-------------String类-------------- //求两个字符串的最大相同子串 /* 思路: 1.找出较短的那个字符串 2.找出短串的所有子串,使用contains函数判断是 ...

  5. 2017北京国庆刷题Day5 afternoon

    期望得分:100+60+100=260 实际得分:0+60+40=100 设图中有m个环,每个环有si条边,有k条边不在环中 ans= (2^s1 -2)*( 2^s2 -2)* (2^s3 -2)… ...

  6. JAVA多线程提高六:java5线程并发库的应用_线程池

    前面我们对并发有了一定的认识,并且知道如何创建线程,创建线程主要依靠的是Thread 的类来完成的,那么有什么缺陷呢?如何解决? 一.对比new Threadnew Thread的弊端 a. 每次ne ...

  7. 超越icon font

    很久以前,我们如何使用图标? 1.切图 2.拼合(Sprites) 原始社会啊! 后来CSSGagagrunt-css-sprite 字体图标 相见不曾相识 Emoji绘文字 iconfont.cn直 ...

  8. 使用条件注释判断 IE 浏览器版本

    IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...

  9. R1(上)—R关联规则分析之Arules包详解

    Arules包详解 包基本信息 发布日期:2014-12-07 题目:挖掘关联规则和频繁项集 描述:提供了一个表达.处理.分析事务数据和模式(频繁项集合关联规则)的基本框架. URL:http://R ...

  10. UIDynamicBehavior的行为类翻译

    CHENYILONG Blog UIDynamicBehavior的行为类翻译 © chenyilong. Powered by Postach.io Blog