[从头学python] 第04节 集合模块collections

本文主要介绍了Python中的collections模块,重点讲解了其在实际编程中的应用,包括fileinput和importlib模块的使用,以及如何利用abc抽象基础类作为mixin增强类的功能。
摘要由CSDN通过智能技术生成
本节目标:
(1) 学习标准库abc, binascii, binhex, builtins, calendar, chunk, collections, formatter,

fileinput, importlib模块




实现步骤:

abc--抽象基础类可以作为mixin,带着它的功能投入到任何类中间去。

>>> import abc
>>> help(abc)
Help on module abc:

NAME
    abc - Abstract Base Classes (ABCs) according to PEP 3119.

CLASSES
    builtins.classmethod(builtins.object)
        abstractclassmethod
    builtins.property(builtins.object)
        abstractproperty
    builtins.staticmethod(builtins.object)
        abstractstaticmethod
    builtins.type(builtins.object)
        ABCMeta
    
    class ABCMeta(builtins.type)
     |  Metaclass for defining Abstract Base Classes (ABCs).
     |  
     |  Use this metaclass to create an ABC.  An ABC can be subclassed
     |  directly, and then acts as a mix-in class.  You can also register
     |  unrelated concrete classes (even built-in classes) and unrelated
     |  ABCs as 'virtual subclasses' -- these and their descendants will
     |  be considered subclasses of the registering ABC by the built-in
     |  issubclass() function, but the registering ABC won't show up in
     |  their MRO (Method Resolution Order) nor will method
     |  implementations defined by the registering ABC be callable (not
     |  even via super()).
     |  
     |  Method resolution order:
     |      ABCMeta
     |      builtins.type
     |      builtins.object
	 
>>> dir(abc)
['ABCMeta', 'WeakSet', '__builtins__', '__cached__', '__doc__', '__file__', 
'__initializing__', '__loader__', '__name__', '__package__', 'abstractclassmethod', 
'abstractmethod', 'abstractproperty', 'abstractstaticmethod']

binascii可以在二进制和ascii中间转换

>>> import binascii
>>> help(binascii)
Help on built-in module binascii:

NAME
    binascii - Conversion between binary data and ASCII

CLASSES
    builtins.Exception(builtins.BaseException)
        Incomplete
    builtins.ValueError(builtins.Exception)
        Error
    
    class Error(builtins.ValueError)
     |  Method resolution order:
     |      Error
     |      builtins.ValueError
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
	 
>>> dir(binascii)
['Error', 'Incomplete', '__doc__', '__loader__', '__name__', '__package__', 'a2b_base64', 
'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', 'b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 
'b2a_uu', 'crc32', 'crc_hqx', 'hexlify', 'rlecode_hqx', 'rledecode_hqx', 'unhexlify']


binhex是文件格式的压缩/解压缩用的,和binascii完全没有相同点

>>> help(binhex)
Help on module binhex:

NAME
    binhex - Macintosh binhex compression/decompression.

DESCRIPTION
    easy interface:
    binhex(inputfilename, outputfilename)
    hexbin(inputfilename, outputfilename)

CLASSES
    builtins.Exception(builtins.BaseException)
        Error
    
    class Error(builtins.Exception)
     |  Method resolution order:
     |      Error
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  

>>> dir(binhex)
['BinHex', 'Error', 'FInfo', 'HexBin', 'LINELEN', 'REASONABLY_LARGE', 'RUNCHAR', '_DID_DATA', 
'_DID_HEADER', '_DID_RSRC', '_Hqxcoderengine', '_Hqxdecoderengine', '_Rlecoderengine', 
'_Rledecoderengine', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', 
'__initializing__', '__loader__', '__name__', '__package__', 'binascii', 'binhex', 
'getfileinfo', 'hexbin', 'io', 'openrsrc', 'os', 'struct']	 

builtins是不需要加载的嫡系内置函数,已经在前几节仔细看过

>>> import builtins
>>> help(builtins)
Help on built-in module builtins:

NAME
    builtins - Built-in functions, exceptions, and other objects.

DESCRIPTION
    Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.

CLASSES
    object
        BaseException
            Exception
                ArithmeticError
                    FloatingPoi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值