3. 不太重要的内建函数

有几个内建的函数在现代Python编程中已经没有必要再学习、知道和使用了。这里保留它们是为了保持为老版本Python编写的程序的向后兼容。

Python程序员、培训人员、学生以及书籍编写人员应该自由跳过这些函数而不用担心遗漏重要的内容。

apply(function, args[, keywords]) The function argument must be a callable object (a user-defined or built-in function or method, or a class object) and the args argument must be a sequence.The function is called with args as the argument list;the number of arguments is the length of the tuple.If the optional keywords argument is present, it must be a dictionary whose keys are strings.It specifies keyword arguments to be added to the end of the argument list.Calling apply() is different from just calling function(args), since in that case there is always exactly one argument.The use of apply() is equivalent to function(args,*keywords).

自2.3版后废弃:使用function(args,**keywords)来代替apply(function,args,keywords)(参见[Unpacking Argument Lists*](#))。

buffer(object[, offset[, size]]) object参数必须是一个支持缓冲区调用接口的对象(例如字符串、数组和缓冲区)。将创建一个新的引用object参数的缓冲区对象。缓冲区对象将是一个从object的起点开始的切片(或者从指定的offset)。切片将扩展到object对象的末尾(或者通过size参数指定的长度)。

coerce(x, y) Return a tuple consisting of the two numeric arguments converted to a common type, using the same rules as used by arithmetic operations.If coercion is not possible, raise TypeError.

intern(string) Enter string in the table of “interned” strings and return the interned string – which is string itself or a copy.Interning strings is useful to gain a little performance on dictionary lookup – if the keys in a dictionary are interned, and the lookup key is interned, the key comparisons (after hashing) can be done by a pointer compare instead of a string compare.Normally, the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys.

2.3版中的变化:Interned strings are not immortal (like they used to be in Python 2.2 and before);you must keep a reference to the return value of intern() around to benefit from it.

脚注

[1] 它极少被使用,所以不应该成为语句。
[2] 当前在没有setvbuf()的系统上指明缓冲大小是没有效果的。该接口指明缓冲大小不是通过调用setvbuf()函数来实现的,因为在执行任意的I/O操作后再调用该函数可能会导致内存转储,同时也没有一个可靠的方法来判断这种情况。

| [3] | In the current implementation, local variable bindings cannot normally be affected this way, but variables retrieved from other scopes (such as modules) can be.This may change. | |-----|-----|