没有合适的资源?快使用搜索试试~ 我知道了~
首页Python 3.3标准库详览与实用查询指南
Python 3.3标准库详览与实用查询指南
4星 · 超过85%的资源 需积分: 33 313 下载量 91 浏览量
更新于2024-07-23
3
收藏 5.65MB PDF 举报
本资源是一份关于Python 3.3标准库的详细查询手册,由Guido van Rossum编写并由Fred L. Drake Jr.编辑,发布于2014年5月22日。Python Software Foundation提供,适用于英语阅读能力稍强的开发者。手册内容涵盖了丰富的知识点,包括但不限于:
1. **简介**:介绍手册的目的和在Python开发中的重要性,以及如何通过手册查找标准库中的函数、类、模块和方法。
2. **内置函数**:详细列出Python 3.3中预定义的一系列核心函数,如布尔测试(如`and`, `or`, `not`)、比较操作、数值类型(如`int`, `float`, `complex`)、迭代器和序列类型(如`list`, `tuple`, `range`),以及文本处理相关的函数。
3. **内置常量**:介绍了由site模块添加的一些特定常量。
4. **内置类型**:这部分详细讨论了各种内置数据类型,如集合类型(`set`, `frozenset`)、映射类型(`dict`),以及特殊的对象属性。
5. **内置异常**:提供了对Python异常体系结构的深入解析,包括基本异常类、具体异常和警告系统。
6. **文本处理服务**:涉及字符串操作(`string`模块)、正则表达式(`re`模块)、计算差异(`difflib`模块)、文本处理(`textwrap`模块)以及Unicode数据处理(`unicodedata`模块)。
这份手册是Python开发者的宝典,对于想要深入了解Python标准库和其功能的开发者来说,它提供了关键的信息和参考资料,无论是在开发过程中遇到问题,还是想要扩展自己的知识库,都是不可或缺的工具。通过查阅手册中的具体内容,开发者能够更高效地利用Python的内建功能,提升编程效率。
The Python Library Reference, Release 3.4.1
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python
int object, it has to define an __index__() method that returns an integer.
bool([x ])
Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns
False; otherwise it returns True. bool is also a class, which is a subclass of int (see Numeric Types —
int, float, complex). Class bool cannot be subclassed further. Its only instances are False and True (see
Boolean Values).
bytearray([source[, encoding[, errors ]]])
Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x <
256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well
as most methods that the bytes type has, see Bytes and Bytearray Operations.
The optional source parameter can be used to initialize the array in a few different ways:
•If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray()
then converts the string to bytes using str.encode().
•If it is an integer, the array will have that size and will be initialized with null bytes.
•If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to
initialize the bytes array.
•If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as
the initial contents of the array.
Without an argument, an array of size 0 is created.
See also Binary Sequence Types — bytes, bytearray, memoryview and Bytearray Objects.
bytes([source[, encoding[, errors ]]])
Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256.
bytes is an immutable version of bytearray – it has the same non-mutating methods and the same
indexing and slicing behavior.
Accordingly, constructor arguments are interpreted as for bytearray().
Bytes objects can also be created with literals, see strings.
See also Binary Sequence Types — bytes, bytearray, memoryview, Bytes, and Bytes and Bytearray Opera-
tions.
callable(object)
Return True if the object argument appears callable, False if not. If this returns true, it is still possible
that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a
class returns a new instance); instances are callable if their class has a __call__() method.
New in version 3.2: This function was first removed in Python 3.0 and then brought back in Python 3.2.
chr(i)
Return the string representing a character whose Unicode codepoint is the integer i. For example, chr(97)
returns the string ’a’. This is the inverse of ord(). The valid range for the argument is from 0 through
1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range.
classmethod(function)
Return a class method for function.
A class method receives the class as implicit first argument, just like an instance method receives the in-
stance. To declare a class method, use this idiom:
class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
6 Chapter 2. Built-in Functions
The Python Library Reference, Release 3.4.1
The @classmethod form is a function decorator – see the description of function definitions in function
for details.
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is
ignored except for its class. If a class method is called for a derived class, the derived class object is passed
as the implied first argument.
Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in
this section.
For more information on class methods, consult the documentation on the standard type hierarchy in types.
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
Compile the source into a code or AST object. Code objects can be executed by exec() or eval().
source can either be a normal string, a byte string, or an AST object. Refer to the ast module documentation
for information on how to work with AST objects.
The filename argument should give the file from which the code was read; pass some recognizable value if
it wasn’t read from a file (’<string>’ is commonly used).
The mode argument specifies what kind of code must be compiled; it can be ’exec’ if source consists of a
sequence of statements, ’eval’ if it consists of a single expression, or ’single’ if it consists of a single
interactive statement (in the latter case, expression statements that evaluate to something other than None
will be printed).
The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the
compilation of source. If neither is present (or both are zero) the code is compiled with those future state-
ments that are in effect in the code that is calling compile. If the flags argument is given and dont_inherit
is not (or is zero) then the future statements specified by the flags argument are used in addition to those
that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future
statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements.
The bitfield required to specify a given feature can be found as the compiler_flag attribute on the
_Feature instance in the __future__ module.
The argument optimize specifies the optimization level of the compiler; the default value of -1 selects
the optimization level of the interpreter as given by -O options. Explicit levels are 0 (no optimization;
__debug__ is true), 1 (asserts are removed, __debug__ is false) or 2 (docstrings are removed too).
This function raises SyntaxError if the compiled source is invalid, and TypeError if the source con-
tains null bytes.
Note: When compiling a string with multi-line code in ’single’ or ’eval’ mode, input must be
terminated by at least one newline character. This is to facilitate detection of incomplete and complete
statements in the code module.
Changed in version 3.2: Allowed use of Windows and Mac newlines. Also input in ’exec’ mode does not
have to end in a newline anymore. Added the optimize parameter.
complex([real[, imag ]])
Create a complex number with the value real + imag*j or convert a string or number to a complex number.
If the first parameter is a string, it will be interpreted as a complex number and the function must be called
without a second parameter. The second parameter can never be a string. Each argument may be any
numeric type (including complex). If imag is omitted, it defaults to zero and the function serves as a
numeric conversion function like int() and float(). If both arguments are omitted, returns 0j.
Note: When converting from a string, the string must not contain whitespace around the central + or -
operator. For example, complex(’1+2j’) is fine, but complex(’1 + 2j’) raises ValueError.
The complex type is described in Numeric Types — int, float, complex.
delattr(object, name)
This is a relative of setattr(). The arguments are an object and a string. The string must be the name
7
The Python Library Reference, Release 3.4.1
of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For
example, delattr(x, ’foobar’) is equivalent to del x.foobar.
dict(**kwarg)
dict(mapping, **kwarg)
dict(iterable, **kwarg)
Create a new dictionary. The dict object is the dictionary class. See dict and Mapping Types — dict for
documentation about this class.
For other containers see the built-in list, set, and tuple classes, as well as the collections module.
dir([object ])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return
a list of valid attributes for that object.
If the object has a method named __dir__(), this method will be called and must return the list of
attributes. This allows objects that implement a custom __getattr__() or __getattribute__()
function to customize the way dir() reports their attributes.
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. The resulting list is not necessarily complete, and
may be inaccurate when the object has a custom __getattr__().
The default dir() mechanism behaves differently with different types of objects, as it attempts to produce
the most relevant, rather than complete, information:
•If the object is a module object, the list contains the names of the module’s attributes.
•If the object is a type or class object, the list contains the names of its attributes, and recursively of the
attributes of its bases.
•Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and re-
cursively of the attributes of its class’s base classes.
The resulting list is sorted alphabetically. For example:
>>> 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’]
Note: Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to
supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of
names, and its detailed behavior may change across releases. For example, metaclass attributes are not in
the result list when the argument is a class.
divmod(a, b)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and
remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators
apply. For integers, the result is the same as (a // b, a % b). For floating point numbers the result is
(q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. In any case q
*
8 Chapter 2. Built-in Functions
The Python Library Reference, Release 3.4.1
b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b)
< abs(b).
enumerate(iterable, start=0)
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports
iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing
a count (from start which defaults to 0) and the values obtained from iterating over 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’)]
Equivalent to:
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
eval(expression, globals=None, locals=None)
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If
provided, locals can be any mapping object.
The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition
list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is
present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed.
This means that expression normally has full access to the standard builtins module and restricted
environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both
dictionaries are omitted, the expression is executed in the environment where eval() is called. The return
value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:
>>> x = 1
>>> eval(’x+1’)
2
This function can also be used to execute arbitrary code objects (such as those created by compile()). In
this case pass a code object instead of a string. 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. If
it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error
occurs).
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.
In all cases, if the optional parts are omitted, the code is executed in the current scope. If only globals is
provided, it must be a dictionary, which will be used for both the global and the local variables. If globals
1
Note that the parser only accepts the Unix-style end of line convention. If you are reading the code from a file, make sure to use newline
conversion mode to convert Windows or Mac-style newlines.
9
The Python Library Reference, Release 3.4.1
and locals are given, they are used for the global and local variables, respectively. If provided, locals can
be any mapping object. Remember that at module level, globals and locals are the same dictionary. 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().
Note: The built-in functions globals() and locals() return the current global and local dictionary,
respectively, which may be useful to pass around for use as the second and third argument to exec().
Note: The default locals act as described for function locals() below: modifications to the default
locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the
code on locals after function exec() returns.
filter(function, iterable)
Construct an iterator from those elements of iterable for which function returns true. iterable may be either
a sequence, a container which supports iteration, or an iterator. 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.
float([x ])
Convert a string or a number to floating point.
If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and option-
ally embedded in whitespace. The optional sign may be ’+’ or ’-’; a ’+’ sign has no effect on the value
produced. The argument may also be a string representing a NaN (not-a-number), or a positive or nega-
tive infinity. More precisely, the input must conform to the following grammar after leading and trailing
whitespace characters are removed:
sign ::= “+” | “-“
infinity ::= “Infinity” | “inf”
nan ::= “nan”
numeric_value ::= floatnumber | infinity | nan
numeric_string ::= [sign] numeric_value
Here floatnumber is the form of a Python floating-point literal, described in floating. Case is not sig-
nificant, so, for example, “inf”, “Inf”, “INFINITY” and “iNfINity” are all acceptable spellings for positive
infinity.
Otherwise, if the argument is an integer or a floating point number, a floating point number with the same
value (within Python’s floating point precision) is returned. If the argument is outside the range of a Python
float, an OverflowError will be raised.
For a general Python object x, float(x) delegates to x.__float__().
If no argument is given, 0.0 is returned.
Examples:
>>> float(’+1.23’)
1.23
>>> float(’ -12345\n’)
10 Chapter 2. Built-in Functions
剩余1625页未读,继续阅读
Orylight
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Angular程序高效加载与展示海量Excel数据技巧
- Argos客户端开发流程及Vue配置指南
- 基于源码的PHP Webshell审查工具介绍
- Mina任务部署Rpush教程与实践指南
- 密歇根大学主题新标签页壁纸与多功能扩展
- Golang编程入门:基础代码学习教程
- Aplysia吸引子分析MATLAB代码套件解读
- 程序性竞争问题解决实践指南
- lyra: Rust语言实现的特征提取POC功能
- Chrome扩展:NBA全明星新标签壁纸
- 探索通用Lisp用户空间文件系统clufs_0.7
- dheap: Haxe实现的高效D-ary堆算法
- 利用BladeRF实现简易VNA频率响应分析工具
- 深度解析Amazon SQS在C#中的应用实践
- 正义联盟计划管理系统:udemy-heroes-demo-09
- JavaScript语法jsonpointer替代实现介绍
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功