Python: class int

 

 

 

 

从上述报错,可知int类参数必须是 str, bytes, bytearray, int, float 五种类型

int() 可接受两个参数 base默认是10进制

  1. 无参 -> zero
  2. 第一参数为 number类型

     

     TypeError 不能带明确的base, 即base就是默认10进制

     

     可将float转为int

  3. 第一参数为 str bytes bytearray 非number类型
    则其必须representing an interger literal in the given base, 即必须是指定base的字面值

     

     

    不能为小数literal

     

     
    base=0 会根据literal智能选择base 即Python, int前缀 0b 0o 0x

     

     

  4. class method

     

     

     

     

     

     

    Syntax

    int.from_bytes(bytes, byteorder, *, signed=False)

     

     x86为little endian, b=b'\x21\x19' 是我们的literal, 在内存中是 '\x19\x21'存储的, 故使用 byteorder='big' 可得到字面值, 而如果使用 'little' 则表示 '\x21\x19' 即使内存中实际存储格式, 其实际值为 '\x19\x21'

    signed=False (default) 默认当作 unsigned解析

     

     

  5. int.to_bytes()     实例方法

     

     

     

     

     

     

 

posted @ 2022-02-25 11:23  ascertain  阅读(1005)  评论(0编辑  收藏  举报