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. Netfilter之连接跟踪实现机制初步分析

    Netfilter之连接跟踪实现机制初步分析 原文: http://blog.chinaunix.net/uid-22227409-id-2656910.html 什么是连接跟踪 连接跟踪(CONNT ...

  2. [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes

    引言 Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧. 例题 1 Given a 2D board cont ...

  3. 分享 koa + mysql 的开发流程,构建 node server端,一次搭建个人博客

    前言 由于一直在用 vue 写业务,为了熟悉下 react 开发模式,所以选择了 react.数据库一开始用的是 mongodb,后来换成 mysql 了,一套下来感觉 mysql 也挺好上手的.re ...

  4. 图论&数学:最小平均值环

    POJ2989:求解最小平均值环 最优化平均值的显然做法是01分数规划 给定一个带权有向图 对于这个图中的每一个环 定义这个环的价值为权值之和的平均值 对于所有的环,求出最小的平均值 这个结论怎么做的 ...

  5. HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模

    LINK 题意:求满足模p下$\frac{1}{a_i+a_j}\equiv\frac{1}{a_i}+\frac{1}{a_j}$的对数,其中$n,p(1\leq n\leq10^5,2\leq p ...

  6. 课程设计——利用信号量实现读-写者问题(JAVA)

    package cn.Douzi.ReadWriter; import java.util.Scanner; public class ReadWrite { static public int co ...

  7. JAVA多线程基础学习一:基础知识

    我们知道多线程是Java编程中重要的一块内容,也是面试重点覆盖区域,所以学好多线程对我们来说极其重要,下面跟我一起开启本次的学习之旅吧. 一.线程基本概念 1 线程:进程中负责程序执行的执行单元(执行 ...

  8. JVM学习二:JVM之类加载器之加载分析

    前面一遍,我们对类的加载有了一个整体的认识,而这一节我们细节分析一下类加载器的第一步,即:加载. 一.概念 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区 ...

  9. 【CODEVS】1922 骑士共存问题

    [算法]二分图最大匹配(最大流) [题解]按(i+j)奇偶性染色后,发现棋子跳到的地方刚好异色. 然后就是二分图了,对于每个奇点向可以跳到的地方连边,偶点不需连(可逆). 所以题目要求转换为求二分图上 ...

  10. Please move or remove them before you can merge

    在使用git pull时,经常会遇到报错: Please move or remove them before you can merge 这是因为本地有修改,与云端别人提交的修改冲突,又没有merg ...