python基础教程之内建函数
2. 内建函数¶
Python解释器包含了一系列可供使用的内置函数,下方表格按字母顺序列出内置函数.
- abs(x)¶
-
返回一个数的绝对值。参数可以是整数或浮点数,如果参数是一个复数,将会返回它的模
- all(iterable)¶
-
如果 iterable(迭代器) 中所有元素都为 true (或者iterable 为空)则返回 True . 相当于
def all(iterable): for element in iterable: if not element: return False return True
- any(iterable)¶
-
假如iterable 里任意一个元素是True,则返回True。假如iterable是空, 返回 False. 相当于:
def any(iterable): for element in iterable: if element: return True return False
- bin(x)¶
-
将一个整数转换成一个二进制字符串。结果是一个有效的Python表达式。假如 x 不是一个 Python int 类型的对象, 那它必须定义一个 __index__() 方法,方法的返回值是整数(integer)
- class bool([x])¶
-
返回一个布尔值, True 或者False之一. x 是使用标准转换的真值测试器. 如果x 的值为假或者省略, 将返回 False; 其它的输入则返回 True. bool 类是 int类的一个子类 (参见 Numeric Types — int, float, complex). 他不能产生子类. 只能实例化为布尔值 False 或者 True (参见 Boolean Values).
- class bytearray([source[, encoding[, errors]]])¶
-
返回一个新的字节数组。bytearray类是一个存储整形数据的可变序列,范围是 0 <= x < 256。该类包含可变序列大多数常用的方法,见Mutable Sequence Types,也包含 bytes类型大多数的方法,见 Bytes and Bytearray Operations。
可选参数source可用于初始化数组,有多种实现方式:
- 如果是个string,必须设置encoding参数( errors可设置也可不设置);然后bytearray()使用str.encode()将字符串转换成字节。
- 如果是个integer,数组将会获取大小并将其初始化为空字节。
- 如果是个符合buffer接口的对象,该对象的一个只读缓冲区将用于初始化字节数组。
- 如果是个 iterable,必须是一个在0 <= x < 256范围内的可迭代的整形数据,并作为数组的初始值。
如果没有设置任何参数,数组大小为0.
参见 Binary Sequence Types — bytes, bytearray, memoryview 和Bytearray Objects.
- class bytes([source[, encoding[, errors]]])¶
-
返回一个新的“bytes”对象,它是一个由0 <= x < 256 范围内的整数组成的不可变序列。bytes 是bytearray 的不可变版 —— 它拥有相同的不可变方法和同样的索引和切片行为。
因此,其构造函数的参数与bytearray() 一样。
字节对象还可以使用字面量创建,参见字符串和字节字面量。
参见 Binary Sequence Types — bytes, bytearray, memoryview, Bytes, 和 Bytes and Bytearray Operations.
- callable(object)¶
-
如果 object 参数可调用,返回True;否则返回False。如果返回True,仍有可能调用失败,但是如果返回False,调用object将永不成功。注意到类是可被调用的(调用一个类返回一个新的实例);如果它们的类有个__call__()方法,实例就可赎回。
3.2版本新变化:这个函数在Python 3.0中首次移除,但在Python 3.2中又添加进来。
- chr(i)¶
-
返回一个Unicode编码是整数i的字符串,例如, chr(97)返回字符串 'a'。这个功能与ord()相反。这个参数正确的范围从0 到1,114,111 (十六进制表示是0x10FFFF ). 如果i超出这个范围,将会触发ValueError。
- classmethod(function)¶
-
将function包装成类方法。
类方法接受类作为隐式的第一个参数,就像实例方法接受实例作为隐式的第一个参数一样。声明一个类方法,使用这样的惯例:
class C: @classmethod def f(cls, arg1, arg2, ...): ...
@classmethod是函数decorator(装饰器)参见Function definitions中的函数定义。
它即可以通过类来调用(如C.f()),也可以通过实例来调用(如C().f())。 除了实例的类,实例本身被忽略。 如果在子类上调用类方法,子类对象被传递为隐式的第一个参数。
类方法不同于C++或Java中的静态方法。 如果你希望使用静态方法,参见这节的staticmethod()。
需要类方法更多的信息,参见The standard type hierarchy中标准类型层次部分的文档。
- compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)¶
-
将source编译成代码或者AST(Abstract Syntax Tree,抽象语法树)对象。 代码对象可以经由exec()语句执行,或者通过调用eval()演算。 source 可以是一个普通字符串,或一个字节字符串,或一个 AST 对象。参考ast模块文档以了解如何和AST对象一起使用的信息。
filename参数指明一个文件,从该文件中读取(源)代码;如果代码不是从文件中读入,传递一个可识别的值(一般用'<string>')。
mode参数指明了编译成哪一类的代码;它可以是 'exec' ,如果 source 包含语句序列;'eval',如果它包含单一表达式;或者是'single',如果它包含一个单一的交互语句 (在后者情况下,表达式计算出的结果除了None之外都会被打印上屏)。
可选的参数flags和dont_inherit控制哪些future语句(见PEP 236)影响source的编译。如果二者都不存在(或两者都是零),代码将与实际上调用compile()的代码中的那些future语句一起被编译。
如果给出了flags参数且没有给出dont_inherit参数(或者为0),除了本该使用的future语句之外,由flags参数指明的future语句也会影响编译。如果dont_inherit是非0整数,flags参数被忽略(调用compile周围的有效的future语句被忽略)。future语句由bit位指明,这些bit可以做或运算,以指明多个语句。可以在__future__模块中,_Feature实例的compiler_flag属性找到指明功能的bit位。
参数 optimize 指定了编译器的优化级别;默认值为 -1 ,选择解释器的优化级别,由 -O options给定。 各级别分别是 0 (不优化; __debug__ is true), 1 (断言被移除 __debug__ is false) 或者 2 (说明文档也被移除)。
这个函数抛出 SyntaxError 异常,如果已编译的源无效;和 TypeError 异常,如果源包含 null 字节。
如果你想要将 Python 代码解析为其 AST 表示形式,请参阅 ast.parse() 。
注意
当以'single'或者'eval'模式编译多行代码字符串的时候,输入至少以一个新行结尾。这主要是便于code模块检测语句是否结束。
Changed in version 3.2: 允许使用Windows和Mac的新行字符。'exec'模式下的输入不需要以新行结束。Added the optimize parameter.
- class complex([real[, imag]])¶
-
返回一个值为 real + imag*j 的复数,或者将一个字符串或数值转换为一个复数。如果第一个参数是个字符串,它将被解释成复数,同时函数不能有第二个参数。第二个参数不能是字符串。每个参数必须是数值类型(包括复数)。如果省略 imag,该参数将被设置为默认值 0,构造函数将像 int 和 float 的数值转换那样进行处理。如果两个参数都被忽略,返回0j。
注意
当从字符串转化成复数的时候,字符串中+或者-两边不能有空白。例如,complex('1+2j') 会正确执行,但 complex('1 + 2j') 会抛出 ValueError 异常。
复数类型在 Numeric Types — int, float, complex中描述。
- delattr(object, name)¶
-
这个函数和setattr()有关。参数是一个对象和一个字符串。 字符串必须是该对象的某个属性的名称。只要对象允许,这个函数删除该名字对应的属性。例如,delattr(x, 'foobar')等同于del x.foobar。
- class dict(**kwarg)
- class dict(mapping, **kwarg)
- class dict(iterable, **kwarg)
-
创建一个新的字典。dict对象就是字典类。参见dict和Mapping Types — dict以得到关于该类的文档。
For other containers see the built-in list, set, and tuple classes, as well as the collections module.
- dir([object])¶
-
参数缺省情况下,返回当前域中所有的变量名和方法名,已经引入的模块名。有一个参数的话,尝试返回由该参数对象的有效属性组成的列表。
如果对象中没有 __dir__()方法, 调用此方法再被调用后将会返回属性列表. 这允许实现了定制化的__getattr__()或者__getattribute__()函数的对象定制dir()报告对象属性的方式。
If the object does not provide __dir__(), the function tries its best to gather information from the object’s __dict__ attribute, if defined, and from its type object. 结果列表没有必要是完整的,如果对象有定制化的__getattr__(),结果还有可能是不准确的。
对于不同类型的对象,默认的dir()行为也不同,因为它尝试产生相关的而不是完整的信息:
- 如果对象是模块对象,列表包含模块的属性名。
- 如果对象是类型或者类对象,列表包含类的属性名,及它的基类的属性名。
- 否则,列表包含对象的属性名,它的类的属性名和类的基类的属性名。
返回的列表按字母顺序排序。例如:
>>> import struct >>> dir() # show the names in the module namespace ['__builtins__', '__name__', 'struct'] >>> dir(struct) # show the names in the struct module ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from'] >>> class Shape: ... def __dir__(self): ... return ['area', 'perimeter', 'location'] >>> s = Shape() >>> dir(s) ['area', 'location', 'perimeter']
注意
因为dir()主要是为了在交互式环境下使用方便,它尝试提供有意义的名字的集合,而不是提供严格或一致定义的名字的集合,且在不同的版本中,具体的行为也有所变化。例如,如果参数是一个类,那么元类属性就不会出现在结果中。
- divmod(a, b)¶
-
参数为两个数值(非复数),返回它们相除后的的商和余数。对于混合的操作数类型,应用二元算术运算符的规则。对于整数,结果等同于 (a // b, a % b)。对于浮点数结果是(q, a % b),q一般是math.floor(a / b),但也可能比那小1。不管怎样,q * b + a % b非常接近于a,如果a % b非0,则余数的符号和b相同,而且0 <= abs(a % b) < abs(b)。
- enumerate(iterable, start=0)¶
-
返回一个枚举对象。iterable 必须是序列、迭代器,或者其他支持迭代的对象。由 enumerate() 返回的迭代器的 __next__() 方法返回一个元组,该元组包括一个计数器 (从 start 开始,start 默认值为0) 和对 iterable 的迭代中取得的值。
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
等于:
def enumerate(sequence, start=0): n = start for elem in sequence: yield n, elem n += 1
- eval(expression, globals=None, locals=None)¶
-
参数是一个字符串参数和可选的 globals 和 locals 两个参数。如果提供 globals 参数,则该参数必须是一个字典。如果提供 locals参数,则该参数可以是任何映射类型的对象。
expression参数被当作Python表达式来解析并演算(技术上来说,是个条件列表),使用globals和locals字典作为全局和局部的命名空间。如果globals字典存在,且缺少‘__builtins__’,在expression被解析之前,当前的全局变量被拷贝进globals。This means that expression normally has full access to the standard builtins module and restricted environments are propagated. 如果locals字典被忽略,默认是globals字典。如果都被忽略,表达式在eval()被调用的环境中执行。返回值是被演算的表达式的结果。语法错误报告成异常。例子:
>>> x = 1 >>> eval('x+1') 2
该函数也能执行任意的代码对象(如compile()返回的结果)。 在这种情况下,传递代码对象而不是字符串。If the code object has been compiled with 'exec' as the mode argument, eval()‘s return value will be None.
Hints: dynamic execution of statements is supported by the exec() function. The globals() and locals() functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or exec().
See ast.literal_eval() for a function that can safely evaluate strings with expressions containing only literals.
- exec(object[, globals[, locals]])¶
-
This function supports dynamic execution of Python code. object must be either a string or a code object. 如果它是一个字符串,该字符串将被当做Python 语句组解析,然后执行(除非发生语法错误)。[1] If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.
在所有情况下,如果可选的部分被省略,代码将在当前的作用域中执行。If only globals is provided, it must be a dictionary, which will be used for both the global and the local variables. If globals and locals are given, they are used for the global and local variables, respectively. 如果有局部变量,locals可以是任何映射类型对象。记住在模块级别,全局和局部字典是同一个字典。If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition.
If the globals dictionary does not contain a value for the key __builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key. That way you can control what builtins are available to the executed code by inserting your own __builtins__ dictionary into globals before passing it to exec().
- filter(function, iterable)¶
-
Construct an iterator from those elements of iterable for which function returns true. iterable可以是个序列,支持迭代的容器,或者一个迭代器。If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.
- class float([x])¶
-
返回一个由数值或字符串 x 转换而成的浮点数。
如果参数是一个字符串,它应该包含一个十进制数,可以有前导符号,周围可以有空白(译者注:空格、\n、\t 等)。前导符号可以是 '+' 或 '-';一个 '+' 符号对结果没有影响。参数也可以是 NaN(非数值)字符串形式,或正负无穷大。更准确地说,输入的内容在去除首尾空格后,必须符合以下语法:
sign ::= "+" | "-" infinity ::= "Infinity" | "inf" nan ::= "nan" numeric_value ::= floatnumber | infinity | nan numeric_string ::= [sign] numeric_value
这里的 floatnumber 是 Python 的浮点数字面值形式,在 浮点数字面值 中有描述。这里不区分大小写,因此,例如“inf”、“Inf”、“INFINITY”和“iNfINity”都是正无穷大的正确拼写。
否则,如果参数是一个整数或浮点数,浮点数作为原值(在 Python 的浮点数精度范围内)返回。如果参数超出 Python 的浮点数范围,将会抛出 OverflowError 异常。
对于一般的 Python 对象 x,float(x) 代表 x.__float__()。
如果省略参数,则返回 0.0。
例子:
>>> float('+1.23') 1.23 >>> float(' -12345\n') -12345.0 >>> float('1e-003') 0.001 >>> float('+1E6') 1000000.0 >>> float('-Infinity') -inf
float 类型描述参见:数值类型 — int, float, complex。
- format(value[, format_spec])¶
-
将value转化成“格式化”的表现形式,格式由format_spec控制。对format_spec的解释依赖于value参数的类型,大多数内置类型有标准的格式化语法:格式化规范迷你语言。
format_spec 的默认值为空字符串,返回的结果通常与 str(value) 相同。
A call to format(value, format_spec) is translated to type(value).__format__(value, format_spec) which bypasses the instance dictionary when searching for the value’s __format__() method. A TypeError exception is raised if the method search reaches object and the format_spec is non-empty, or if either the format_spec or the return value are not strings.
Changed in version 3.4: object().__format__(format_spec) raises TypeError if format_spec is not an empty string.
- class frozenset([iterable])
-
返回一个新的frozenset对象,如果可选参数iterable存在,frozenset的元素来自于iterable。frozenset是个内置类。参见frozenset和Set Types — set, frozenset。
For other containers see the built-in set, list, tuple, and dict classes, as well as the collections module.
- getattr(object, name[, default])¶
-
返回object的属性值。name必须是个字符串。如果字符串是对象某个属性的名字,则返回该属性的值。例如,getattr(x, 'foobar')等同于x.foobar。If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
- globals()¶
-
返回表示当前全局符号表的字典。它总是当前模块的字典(在函数或者方法中,它指定义的模块而不是调用的模块)。
- hasattr(object, name)¶
-
参数是一个对象和一个字符串。The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)
- hash(object)¶
-
返回对象的哈希值. hash值是整数。它被用于在字典查找时快速比较字典的键。相同的数值有相同的hash(尽管它们有不同的类型,比如1和1.0)。
注意
For object’s with custom __hash__() methods, note that hash() truncates the return value based on the bit width of the host machine. See __hash__() for details.
- help([object])¶
-
调用内置的帮助系统。(这个函数主要用于交互式使用。)如果没有参数,在解释器的控制台启动交互式帮助系统。如果参数是个字符串,该字符串被当作模块名,函数名,类名,方法名,关键字或者文档主题而被查询,在控制台上打印帮助页面。如果参数是其它某种对象,生成关于对象的帮助页面。
这个函数经由site模块加入内置的命名空间。
- hex(x)¶
-
将一个整数转换成一个小写的十六进制字符串。结果以“0x”为前缀,例如:
>>> hex(255) '0xff' >>> hex(-42) '-0x2a'
如果 x 不是一个 Python int 类型的对象,则它必须定义一个返回整数的 __index__() 方法。
参见int(),它将十六进制字符串转化成一个整数。
注意
使用float.hex()方法得到浮点数的十六进制字符串表示。
- id(object)¶
-
返回对象的“标识”。This is an integer which is guaranteed to be unique and constant for this object during its lifetime. 生命期不重叠的两个对象可以有相同的id()值。
CPython实现细节: 这是对象的内存地址。
- input([prompt])¶
-
如果有prompt参数,则将它输出到标准输出且不带换行。该函数然后从标准输入读取一行,将它转换成一个字符串(去掉一个末尾的换行符),然后返回它。When EOF is read, EOFError is raised. 例子:
>>> s = input('--> ') --> Monty Python's Flying Circus >>> s "Monty Python's Flying Circus"
- class int(x=0)¶
- class int(x, base=10)
-
返回一个由数值或字符串 x 转换而成的整数,如果省略参数,则返回 0。如果 x 是一个数值,则返回 x.__int__()。如果是浮点数,则会截去小数部分(译者注:原文意思是“向 0 方向取最近的整数”)。
如果 x 不是数值,或者指定了 base 参数,则 x 必须为 string、bytes 或 bytearray 实例,且该实例必须为 base 进制的整数字面量。字面量的前面可以有+或者-(中间不能有空格),周围可以有空白。以n为基数的字面量包含数字0到n-1,用a到z(或者A到Z)来表示10到35。默认的base是10。允许的值为0和2-36。基于2进制、8进制、16进制的整数字面量,可以加上 0b/0B、0o/0O 或 0x/0X 前缀直接用于代码中。基于 0 进制的意思是依据代码中实际的字面量自动判断进位制(译者注:例如 ‘0b10’、’0o10’、’10’、’0x10’),因此实际的进位制是对应的 2、8、10 或 16,因此 int('010', 0) 是非法的(译者注:因为 ‘010’ 以 0 开头却没有指定进位制符号 b、o、x,无法自动判断进位制),虽然 int('010') 和 int('010', 8) 是正确的用法。
整数类型的描述参见:数字类型 — int, float, complex。
Changed in version 3.4: If base is not an instance of int and the base object has a base.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.
- isinstance(object, classinfo)¶
-
如果参数object 是参数classinfo 的一个实例;或者是其一个(直接的、间接的或者virtual)子类的实例,返回真。If object is not an object of the given type, the function always returns false. If classinfo is not a class (type object), it may be a tuple of type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.
- issubclass(class, classinfo)¶
-
如果class是classinfo的子类(直接的,间接的,或者virtual) ,返回真。一个类被认为是它自己的子类。classinfo可以是类对象的元组,这时classinfo中的每个类对象都会被检查。In any other case, a TypeError exception is raised.
- iter(object[, sentinel])¶
-
返回一个iterator对象。根据有无第二个参数,对第一个参数的解释相差很大。Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its __next__() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
See also Iterator Types.
第二种形式的iter()的一个有用的应用就是读一个文件的行,直到读到特定行。下面的例子读一个文件,直到readline()方法返回一个空字符串:
with open('mydata.txt') as fp: for line in iter(fp.readline, ''): process_line(line)
- len(s)¶
-
返回一个对象的长度 (元素的个数) . 参数可以是序列(如字符串,字节,元组,列表或者范围)或者集合(如字典,集合或者固定集合)。
- class list([iterable])
-
Rather than being a function, list is actually a mutable sequence type, as documented in Lists and Sequence Types — list, tuple, range.
- locals()¶
-
更新并返回表示当前局部符号表的字典。当locals在函数块中而不是类块中被调用时,locals()返回自由变量。
注意
不应该修改该字典的内容;所做的改变不一定会影响到解释器所用的局部和自由变量的值。
- map(function, iterable, …)¶
-
Return an iterator that applies function to every item of iterable, yielding the results. 如果有额外的iterable参数,并行的从这些参数中取元素,并调用function。With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().
- max(iterable, *[, key, default])¶
- max(arg1, arg2, *args[, key])
-
返回可迭代的对象中的最大的元素,或者返回2个或多个参数中的最大的参数。
If one positional argument is provided, it should be an iterable. 返回可迭代对象中最大的元素。如果有2个或更多的位置参数,返回最大位置参数。
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).
New in version 3.4: The default keyword-only argument.
- memoryview(obj)
-
返回给定参数的“内存视图”。See Memory Views for more information.
- min(iterable, *[, key, default])¶
- min(arg1, arg2, *args[, key])
-
返回可迭代的对象中的最小的元素,或者返回2个或多个参数中的最小的参数。
If one positional argument is provided, it should be an iterable. 返回可迭代对象中最小的元素。如果有2个或更多的位置参数,返回最小的位置参数。
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.
If multiple items are minimal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc)[0] and heapq.nsmallest(1, iterable, key=keyfunc).
New in version 3.4: The default keyword-only argument.
- next(iterator[, default])¶
-
Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
- class object¶
-
返回一个新的无特征的对象。object is a base for all classes. It has the methods that are common to all instances of Python classes. 改函数不接受任何的参数。
- oct(x)¶
-
将一个整数转换成一个八进制字符串。结果是一个合法的Python表达式。如果x不是一个Python int对象,它必须定义一个返回整数的__index__()方法。
- open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)¶
-
Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.
file is either a string or bytes object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)
mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for exclusive creation and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are:
Character 含义 'r' 读取模式(默认值) 'w' 写入模式,打开时会清空文件内容 'x' 创建模式,如果文件已经存在则执行失败 'a' 追加模式,追加内容到文件的末尾 'b' 二进制模式 't' 文本模式(默认值) '+' 打开磁盘文件用于更新(读和写) 'U' 通用换行符模式(已弃用) The default mode is 'r' (open for reading text, synonym of 'rt'). For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file without truncation.
As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.
注意
Python doesn’t depend on the underlying operating system’s notion of text files; all the processing is done by Python itself, and is therefore platform-independent.
buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size in bytes of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:
- Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE. On many systems, the buffer will typically be 4096 or 8192 bytes long.
- “Interactive” text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.
encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.
errors is an optional string that specifies how encoding and decoding errors are to be handled–this cannot be used in binary mode. A variety of standard error handlers are available (listed under Error Handlers), though any error handling name that has been registered with codecs.register_error() is also valid. The standard names include:
- 'strict' to raise a ValueError exception if there is an encoding error. The default value of None has the same effect.
- 'ignore' ignores errors. Note that ignoring encoding errors can lead to data loss.
- 'replace' causes a replacement marker (such as '?') to be inserted where there is malformed data.
- 'surrogateescape' will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when writing data. This is useful for processing files in an unknown encoding.
- 'xmlcharrefreplace' is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference &#nnn;.
- 'backslashreplace' (also only supported when writing) replaces unsupported characters with Python’s backslashed escape sequences.
newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '
', '
‘, and '
‘1>. It works as foll- When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
- When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.
If closefd is False and a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is closed. If a filename is given closefd has no effect and must be True (the default).
A custom opener can be used by passing a callable as opener. The underlying file descriptor for the file object is then obtained by calling opener with (file, flags). opener must return an open file descriptor (passing os.open as opener results in functionality similar to passing None).
The newly created file is non-inheritable.
The following example uses the dir_fd parameter of the os.open() function to open a file relative to a given directory:
>>> import os >>> dir_fd = os.open('somedir', os.O_RDONLY) >>> def opener(path, flags): ... return os.open(path, flags, dir_fd=dir_fd) ... >>> with open('spamspam.txt', 'w', opener=opener) as f: ... print('This will be written to somedir/spamspam.txt', file=f) ... >>> os.close(dir_fd) # don't leak a file descriptor
The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a subclass of io.TextIOBase (specifically io.TextIOWrapper). When used to open a file in a binary mode with buffering, the returned class is a subclass of io.BufferedIOBase. The exact class varies: in read binary mode, it returns a io.BufferedReader; in write binary and append binary modes, it returns a io.BufferedWriter, and in read/write mode, it returns a io.BufferedRandom. When buffering is disabled, the raw stream, a subclass of io.RawIOBase, io.FileIO, is returned.
See also the file handling modules, such as, fileinput, io (where open() is declared), os, os.path, tempfile, and shutil.
Changed in version 3.3: The opener parameter was added. The 'x' mode was added. IOError used to be raised, it is now an alias of OSError. FileExistsError is now raised if the file opened in exclusive creation mode ('x') already exists.
Changed in version 3.4: The file is now non-inheritable.
Deprecated since version 3.4, will be removed in version 4.0: The 'U' mode.
- ord(c)¶
-
Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord('a') returns the integer 97 and ord('u2020') returns 822416>. This is the inverse of chr()
- pow(x, y[, z])¶
-
返回x 的 y次方; 如果 z 提供的时候,返回 x 的 y 次方,然后对 z取模。(这样比 pow(x, y) % z) 更高效。 pow(x, y)的效果类似于: x**y.
参数必须是数字类型的。作为混合操作数类型,应用二元算术运算符的强制规则(译者注:“强制规则(Coercion rules)”在 Python 3 之后已经废除)。对于 int 操作数,结果与操作数具有相同的类型(强制之后)除非第二个参数是负数;在这种情况下, 所有的参数都会被转化成浮点型,并且会返回一个浮点的结果。例如, 10**2 返回 100, 但 10**-2 返回0.01. 如果第二个参数为负数,那么第三个参数必须省略。如果提供参数 z,则 x 和 y 必须为整数,而且 y 必须是非负整数。
- print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)¶
-
打印 objects 到文本流对象 file,以 sep 为分隔符,以 end 为结尾符。如果要提供 sep, end 和 file 这三个参数的话,必须使用键值对的形式。
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. sep and end 都必须是字符串形式的; they can also be None, which means to use the default values. 如果没有打印 对象, print() 只打印一个 结束符号 end.
file 参数一定要是含有 write(string)方法的对象 ; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.
Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.
Changed in version 3.3: Added the flush keyword argument.
- class property(fget=None, fset=None, fdel=None, doc=None)¶
-
返回一个property 属性。
fget 是一个用于获取属性值的函数。fget 是一个用于设置属性值的函数。fget 是一个用于删除属性值的函数。doc 创建该属性的一个文档字符串。
一种典型用法是定义一个托管属性x:
class C: def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
如果c 是C 的一个实例,则c.x 将调用getx,c.x = value 将调用setx,del c.x 将调用delx。
如果给出doc,它将是该属性的文档字符串。否则,该属性将拷贝fget的文档字符串(如果存在)。这使得用property()作为装饰器创建一个只读属性非常容易:
class Parrot: def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage
@property 装饰器将voltage() 方法变成一个具有相同名称的只读属性,并设置voltage 的文档字符串为”Get the current voltage.”。
Property 对象具有getter、setter 和deleter 方法,它们可以用做装饰器来创建该属性的拷贝,使得相应的访问函数设置给被修饰的函数。最好的解释就是使用一个例子:
class C: def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x
这段代码与第一个例子完全相等。请确保额外的函数与原始的property 具有相同的名字(此例中为x。)
返回的property 对象同样具有与构造函数参数对应的属性fget、fset 和fdel。
- range(stop)
- range(start, stop[, step])
-
区别于函数, range 实际上是一个不可变序列类型, 描述于 Ranges and Sequence Types — list, tuple, range.
- repr(object)¶
-
返回一个字符串,该字符串包含某个对象的可打印表达式。对于许多类型来说,当传递给eval()时,此函数尝试返回一个字符串,该字符串将会生成一个具有相同值的对象,否则返回一个包含在尖括号中的字符串,此字符串包含对象类型的名字和通常包括了对象名字和地址的附加信息。
一个类可以通过定义一个__repr__()方法为其实例控制该函数的返回值。
- reversed(seq)¶
-
返回一个反向迭代器。seq必须是一个具有__reversed__() 方法或支持序列协议的对象(整数参数从0开始的__len__()方法和__getitem__() 方法)。
- round(number[, ndigits])¶
-
返回浮点数 number 四舍五入到小数点之后 ndigits 位的结果。如果省略ndigits,该参数默认为零。Delegates to number.__round__(ndigits).
对于内置类型所支持的 round(), 值被舍入到最接近于 10 的 –ndigits 次幂;if two multiples are equally close, rounding is done toward the even choice (举个例子, round(0.5) 和 round(-0.5) 两者返回值都是 0,而 round(1.5) 则是 2)。 如果调用时只有一个参数,返回值是 int 型;否则和 number 同类型。
注意
浮点数round()的行为可能让人惊讶,例如round(2.675, 2)给出的是2.67 而不是期望的2.68。这不是一个错误:这是一个事实,大部分十进制小数不能用浮点数精确表示。更多信息,请参阅浮点数运算:问题和局限。
- class set([iterable])
-
返回一个新的 set 对象,其元素可以从可选的iterable获得。set是一个内建的类。关于该类的文档,请参阅set和集合类型 — set, frozenset。
其他容器请参阅内置 frozenset,list,tuple,和 dict 类,以及 collections 模块。
- setattr(object, name, value)¶
-
getattr()的相反操作。参数是一个对象、一个字符串和任何一个值。字符串可以是一个已存在属性的名字也可以是一个新属性的名字。该函数将值赋值给属性,只要对象允许。例如,setattr(x, 'foobar', 123)等同于x.foobar = 123。
- class slice(stop)¶
- class slice(start, stop[, step])
-
返回一个slice对象,表示由索引range(start, stop, step)指出的集合。The start and step arguments default to None. 切片对象具有只读属性start、stop和step,它们仅仅返回参数的值(或者它们的默认值)。它们没有其他显式的函数;它是它们用于Numerical Python和其它第三方扩展。在使用扩展的索引语法时同样会生成切片对象。例如:a[start:stop:step]或a[start:stop, i]。返回迭代器的另外一个版本可以参阅itertools.islice()。
- sorted(iterable[, key][, reverse])¶
-
依据iterable中的元素返回一个新的列表。
Has two optional arguments which must be specified as keyword arguments.
key指定一个带有一个参数的函数,它用于从每个列表元素选择一个比较的关键字:key=str.lower。The default value is None (compare the elements directly).
reverse是一个布尔值。If set to True, then the list elements are sorted as if each comparison were reversed.
使用functools.cmp_to_key()来转换旧式的cmp函数为key函数。
The built-in sorted() function is guaranteed to be stable. 排序是稳定的,如果它保证不会改变比较结果相等的元素的相对顺序 — 这在依据多个途径进行排序时非常有用 (例如,先根据部门然后根据薪酬等级排序)。
For sorting examples and a brief sorting tutorial, see Sorting HowTo.
- staticmethod(function)¶
-
返回function的一个静态方法。
静态方法不接受隐式的第一个参数(也就是实例名称self)。要声明静态方法,请使用下面的习惯方式:
class C: @staticmethod def f(arg1, arg2, ...): ...
@staticmethod形式是一个函数装饰器 – 细节请参阅函数定义中函数定义的描述。
它即可以通过类来调用(如C.f()),也可以通过实例来调用(如C().f())。除了实例的类,实例本身被忽略。
Python中的静态方法类似于Java或C++。关于创建类构造器的另外一种方法,请参阅classmethod()。
更多关于静态方法的信息,请查看标准类型层次中标准类型层次的文档。
- class str(object=”)
- class str(object=b”, encoding=’utf-8′, errors=’strict’)
-
返回 object 的 字符串 版本。详细信息参见 str()。
str 是内建的字符串 类。关于字符串的完整信息,参见 文本序列类型 — str。
- sum(iterable[, start])¶
-
将start以及iterable的元素从左向右相加并返回总和。start默认为0。iterable的元素通常是数字,start值不允许是一个字符串。
对于某些使用场景,有比sum()更好的选择。连接字符串序列的首选和快速的方式是调用''.join(sequence)。如要相加扩展精度的浮点数,请参阅math.fsum()。若要连接一系列的可迭代量,可以考虑使用itertools.chain()。
- super([type[, object-or-type]])¶
-
返回一个代理对象,这个对象指派方法给一个父类或者同类. 这对进入类中被覆盖的继承方法非常有用。搜索顺序和 getattr() 一样。而它自己的 类型 则被忽略。
type的__mro__属性罗列了用getattr()和super()来搜索排序的解决方法。The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).
super 函数有两种典型用例。In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.
The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
For both use cases, a typical superclass call looks like this:
class C(B): def method(self, arg): super().method(arg) # This does the same thing as: # super(C, self).method(arg)
Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].
Also note that, aside from the zero argument form, super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references.The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods.
For practical suggestions on how to design cooperative classes using super(), see guide to using super().
- tuple([iterable])
-
Rather than being a function, tuple is actually an immutable sequence type, as documented in Tuples and Sequence Types — list, tuple, range.
- class type(object)¶
- class type(name, bases, dict)
-
只有一个参数时,返回object的类型。The return value is a type object and generally the same object as returned by object.__class__.
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
带有三个参数时,返回一个新的类型对象。它本质上是class语句的动态形式。name字符串是类的名字且将成为__name__属性;bases元组逐条列举基类并成为__bases__属性;dict字典是包含类体定义的命名空间并成为__dict__属性。例如,下面的两条语句创建完全相同的type对象:
>>> class X: ... a = 1 ... >>> X = type('X', (object,), dict(a=1))
See also Type Objects.
- vars([object])¶
-
返回模块,类,实例或者任何其他有__dict__属性的对象的__dict__属性
对象例如模块和实例有一个可更新的 __dict__ 属性; 然而,其他对象的 __dict__ 属性可能有写限制(例如, classes use a dictproxy to prevent direct dictionary updates)。
- zip(*iterables)¶
-
利用每个可迭代元素,制作一个迭代器来聚合元素。
Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. 等同于:
def zip(*iterables): # zip('ABCD', 'xy') --> Ax By sentinel = object() iterators = [iter(it) for it in iterables] while iterators: result = [] for it in iterators: elem = next(it, sentinel) if elem is sentinel: return result.append(elem) yield tuple(result)
可以保证迭代按从左向右的计算顺序。这使得使用zip(*[iter(s)]*n)来将一系列数据分类归并为长度为n的组成为习惯用法。
zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.
zip() 与 * 操作符一起可以用来 unzip 一个列表:
>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> zipped = zip(x, y) >>> list(zipped) [(1, 4), (2, 5), (3, 6)] >>> x2, y2 = zip(*zip(x, y)) >>> x == list(x2) and y == list(y2) True
- __import__(name, globals=None, locals=None, fromlist=(), level=0)¶
-
注意
与 importlib.import_module() 不同,这是一个高级的函数,不会在日常的 Python 编程中用到。
它是通过 import 语句调用的.It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().
The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name. The standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.
level specifies whether to use absolute or relative imports. 0 (the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__() (see PEP 328 for the details).
When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.
For example, the statement import spam results in bytecode resembling the following code:
spam = __import__('spam', globals(), locals(), [], 0)
The statement import spam.ham results in this call:
spam = __import__('spam.ham', globals(), locals(), [], 0)
Note how __import__() returns the toplevel module here because this is the object that is bound to a name by the import statement.
On the other hand, the statement from spam.ham import eggs, sausage as saus results in
_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0) eggs = _temp.eggs saus = _temp.sausage
Here, the spam.ham module is returned from __import__(). From this object, the names to import are retrieved and assigned to their respective names.
如果你只是简单地想依据名字导入一个模块,请使用importlib.import_module()。
Changed in version 3.3: Negative values for level are no longer supported (which also changes the default value to 0).
脚注
[1] | 注意解析器只接受Unix风格的行结束惯例。If you are reading the code from a file, make sure to use newline conversion mode to convert Windows or Mac-style newlines. |