美文网首页
python3文档标准库解读

python3文档标准库解读

作者: 庄周幻梦 | 来源:发表于2021-01-10 18:27 被阅读0次

    Python 解释器内置了很多函数和类型,您可以在任何时候使用它们。以下按字母表顺序列出它们。


    image.png
    • abs(__x)
      """
      Return the absolute value of a number. The argument may be an integer or a floating point number. If the
      argument is a complex number, its magnitude is returned. If x defines __abs__(), abs(x) returns x.
      __abs__().
      返回数字的绝对值。 参数可以是整数或浮点数。 如果参数是一个复数,返回其大小。 如果x定义了__abs __(),则abs(x)返回x。
      """
      class NewA:
          def __init__(self, parm):
              self.parm = parm
      
          def __abs__(self):
              return self.parm
      
      
      a = abs(-123)                  #  a = 123
      b = abs(123)                   # b = 123
      c = abs(1.23)                  # c = 1.23
      d = abs(-1.23)                 # d = 1.23
      e = abs(NewA(-123))            # e = -123 
      
    • all(__iterable)
    """
    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.
    如果 iterable 的所有元素为真(或迭代器为空),返回 True 。
    """
    s = all([1, True, None, 1.23, -5, [123]])  # s = False
    ss = all([1, True, 1.23, -5, [123]])      # s = True
    # 等价于
    def all(__iterable):
        for element in __iterable:
            if not element:
                return False
            return True
    
    • any(__iterable)
    """
    Return True if bool(x) is True for any x in the iterable.
        If the iterable is empty, return False.
        如果 iterable 的任一元素为真则返回 True。如果迭代器为空,返回 False。
    """
    # 等价于
    
    def any(__iterable):
        for element in __iterable:
            if element:
                return False
            return True
    
    • ascii(__obj)
    """
    >Return an ASCII-only representation of an object.
    返回对象的纯ASCII表示形式。
    """
    ascii('你好') # \u4f60\u597d
    
    • bin(__obj)
    """
    Return the binary representation of an integer.
    返回整数的二进制表示形式。
    """
    bin(2021) # 0b11111100101
    
    • bool(o:object)
    """
    Returns True when the argument x is true, False otherwise.
    The builtins True and False are the only two instances of the class bool.
    The class bool is a subclass of the class int, and cannot be subclassed.
    当参数x为true时返回True,否则返回False。
    内置的True和False是bool类的仅有的两个实例。
    bool类是int类的子类,不能被子类化。
    """
    # bool函数实际上是个类,继承于int类型。
    bool(0) # False
    bool(1) # True
    
    • breakpoint(args,*kwagrs)
    """
    breakpoint(*args, **kws)
    
    Call sys.breakpointhook(*args, **kws).  sys.breakpointhook() must accept
    whatever arguments are passed.
    
    By default, this drops you into the pdb debugger.
    调用sys.breakpointhook(), 默认情况下会进入pdb调试器
    """
    breakpoint()
    
    • class bytearray([source[, encoding[, errors]]])
    """
    bytearray(iterable_of_ints) -> bytearray
    bytearray(string, encoding[, errors]) -> bytearray
    bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer
    bytearray(int) -> bytes array of size given by the parameter initialized with null bytes
    bytearray() -> empty bytes array
    
    Construct a mutable bytearray object from:
      - an iterable yielding integers in range(256)
      - a text string encoded using the specified encoding
      - a bytes or a buffer object
      - any object implementing the buffer API.
      - an integer
    # (copied from class doc)
    返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256。
    如果 source 为整数,则返回一个长度为 source 的初始化数组;
    如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
    如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
    如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
    如果没有输入任何参数,默认就是初始化数组为0个元素。
    """
    bytearray([1, 2, 3]) # bytearray(b'\x01\x02\x03')
    

    ================================================================================
    该文章将会以python3.8官方文档标准库为基准。逐步更新~~

    相关文章

      网友评论

          本文标题:python3文档标准库解读

          本文链接:https://www.haomeiwen.com/subject/zchlaktx.html