企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](design.xhtml "设计和历史常见问题") | - [上一页](general.xhtml "Python常见问题") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 常见问题](index.xhtml) » - $('.inline-search').show(0); | # [编程常见问题](#id2) 目录 - [编程常见问题](#programming-faq) - [一般问题](#general-questions) - [Python 有没有提供断点与单步调试等功能的,源码层次的调试器?](#is-there-a-source-code-level-debugger-with-breakpoints-single-stepping-etc) - [有没有工具来帮助找寻漏洞或进行静态分析?](#is-there-a-tool-to-help-find-bugs-or-perform-static-analysis) - [我如何能够通过一个 Python 脚本创建一个独立运行的二进制文件?](#how-can-i-create-a-stand-alone-binary-from-a-python-script) - [是否有编程标准或Python程序的样式指南?](#are-there-coding-standards-or-a-style-guide-for-python-programs) - [核心语言](#core-language) - [当变量有值时,为什么会出现UnboundLocalError?](#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value) - [Python中的局部变量和全局变量有哪些规则?](#what-are-the-rules-for-local-and-global-variables-in-python) - [为什么在具有不同值的循环中定义的lambdas都返回相同的结果?](#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result) - [如何跨模块共享全局变量?](#how-do-i-share-global-variables-across-modules) - [导入模块的“最佳实践”是什么?](#what-are-the-best-practices-for-using-import-in-a-module) - [为什么对象之间共享默认值?](#why-are-default-values-shared-between-objects) - [如何将可选参数或关键字参数从一个函数传递到另一个函数?](#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another) - [形参和实参之间有什么区别?](#what-is-the-difference-between-arguments-and-parameters) - [为什么更改列表'y'也会更改列表'x'?](#why-did-changing-list-y-also-change-list-x) - [如何编写带输出参数的函数(通过引用调用)?](#how-do-i-write-a-function-with-output-parameters-call-by-reference) - [如何在Python中创建高阶函数?](#how-do-you-make-a-higher-order-function-in-python) - [如何在Python中复制对象?](#how-do-i-copy-an-object-in-python) - [如何找到对象的方法或属性?](#how-can-i-find-the-methods-or-attributes-of-an-object) - [我的代码如何才能发现对象的名称?](#how-can-my-code-discover-the-name-of-an-object) - [逗号运算符的优先级是什么?](#what-s-up-with-the-comma-operator-s-precedence) - [Is there an equivalent of C's "?:" ternary operator?](#is-there-an-equivalent-of-c-s-ternary-operator) - [Is it possible to write obfuscated one-liners in Python?](#is-it-possible-to-write-obfuscated-one-liners-in-python) - [函数参数列表中的斜杠(/)是什么意思?](#what-does-the-slash-in-the-parameter-list-of-a-function-mean) - [数字和字符串](#numbers-and-strings) - [如何指定十六进制和八进制整数?](#how-do-i-specify-hexadecimal-and-octal-integers) - [为什么-22 // 10返回-3?](#why-does-22-10-return-3) - [如何将字符串转换为数字?](#how-do-i-convert-a-string-to-a-number) - [如何将数字转换为字符串?](#how-do-i-convert-a-number-to-a-string) - [如何修改字符串?](#how-do-i-modify-a-string-in-place) - [如何使用字符串调用函数/方法?](#how-do-i-use-strings-to-call-functions-methods) - [Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?](#is-there-an-equivalent-to-perl-s-chomp-for-removing-trailing-newlines-from-strings) - [Is there a scanf() or sscanf() equivalent?](#is-there-a-scanf-or-sscanf-equivalent) - [What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?](#what-does-unicodedecodeerror-or-unicodeencodeerror-error-mean) - [性能](#performance) - [我的程序太慢了。该如何加快速度?](#my-program-is-too-slow-how-do-i-speed-it-up) - [将多个字符串连接在一起的最有效方法是什么?](#what-is-the-most-efficient-way-to-concatenate-many-strings-together) - [序列(元组/列表)](#sequences-tuples-lists) - [如何在元组和列表之间进行转换?](#how-do-i-convert-between-tuples-and-lists) - [什么是负数序号?](#what-s-a-negative-index) - [如何以相反的顺序迭代序列?](#how-do-i-iterate-over-a-sequence-in-reverse-order) - [如何从列表中删除重复项?](#how-do-you-remove-duplicates-from-a-list) - [你如何在Python中创建一个数组?](#how-do-you-make-an-array-in-python) - [如何创建多维列表?](#how-do-i-create-a-multidimensional-list) - [如何将方法应用于一系列对象?](#how-do-i-apply-a-method-to-a-sequence-of-objects) - [Why does a\_tuple\[i\] += \['item'\] raise an exception when the addition works?](#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works) - [I want to do a complicated sort: can you do a Schwartzian Transform in Python?](#i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python) - [如何按其他列表中的值对一个列表进行排序?](#how-can-i-sort-one-list-by-values-from-another-list) - [对象](#objects) - [什么是类?](#what-is-a-class) - [What is a method?](#what-is-a-method) - [什么是self?](#what-is-self) - [如何检查对象是否是给定类或其子类的实例?](#how-do-i-check-if-an-object-is-an-instance-of-a-given-class-or-of-a-subclass-of-it) - [什么是代理?](#what-is-delegation) - [如何从覆盖基类的派生类调用基类中定义的方法?](#how-do-i-call-a-method-defined-in-a-base-class-from-a-derived-class-that-overrides-it) - [如何组织代码以便更改基类?](#how-can-i-organize-my-code-to-make-it-easier-to-change-the-base-class) - [如何创建静态类数据和静态类方法?](#how-do-i-create-static-class-data-and-static-class-methods) - [如何在Python中重载构造函数(或方法)?](#how-can-i-overload-constructors-or-methods-in-python) - [I try to use \_\_spam and I get an error about \_SomeClassName\_\_spam.](#i-try-to-use-spam-and-i-get-an-error-about-someclassname-spam) - [My class defines \_\_del\_\_ but it is not called when I delete the object.](#my-class-defines-del-but-it-is-not-called-when-i-delete-the-object) - [如何获取给定类的所有实例的列表?](#how-do-i-get-a-list-of-all-instances-of-a-given-class) - [为什么 `id()` 的结果看起来不是唯一的?](#why-does-the-result-of-id-appear-to-be-not-unique) - [模块](#modules) - [怎么创建.pyc文件?](#how-do-i-create-a-pyc-file) - [如何找到当前模块名称?](#how-do-i-find-the-current-module-name) - [怎样才能拥有相互导入的模块?](#how-can-i-have-modules-that-mutually-import-each-other) - [\_\_import\_\_('x.y.z') returns <module 'x'>; 如何获取z?](#import-x-y-z-returns-module-x-how-do-i-get-z) - [当我编辑导入的模块并重新导入它时,更改不会显示。为什么会这样?](#when-i-edit-an-imported-module-and-reimport-it-the-changes-don-t-show-up-why-does-this-happen) ## [一般问题](#id3) ### [Python 有没有提供断点与单步调试等功能的,源码层次的调试器?](#id4) 有的。 Several debuggers for Python are described below, and the built-in function [`breakpoint()`](../library/functions.xhtml#breakpoint "breakpoint") allows you to drop into any of them. pdb 模块是一个简单但是够用的控制台模式 Python 调试器。 它是标准 Python 库的一部分,并且 [`已收录于库参考手册`](../library/pdb.xhtml#module-pdb "pdb: The Python debugger for interactive interpreters.")。 你也可以通过使用 pdb 代码作为样例来编写你自己的调试器。 作为标准 Python 发行版附带组件的 IDLE 交互式环境(通常位于 Tools/scripts/idle)中包含一个图形化的调试器。 PythonWin 是一个包含有基于 pdb 的 GUI 调试器的 Python IDE。 Pythonwin 调试器会为断点加上颜色,并具有许多很棒的特性,例如也可以非 Pythonwin 程序。 Pythonwin 是 [Python for Windows Extensions](https://sourceforge.net/projects/pywin32/) \[https://sourceforge.net/projects/pywin32/\] 项目的一部分,也是 ActivePython 发行版的一部分(参见 <https://www.activestate.com/activepython>)。 [Boa Constructor](http://boa-constructor.sourceforge.net/) \[http://boa-constructor.sourceforge.net/\] 是一个使用wxWidgets的IDE和GUI构建器。它提供可视化框架创建和操作,对象检查器,源对象浏览器上的许多视图,继承层次结构,doc字符串生成的html文档,高级调试器,集成帮助和Zope支持。 [Eric](http://eric-ide.python-projects.org/) \[http://eric-ide.python-projects.org/\] 是一个基于PyQt和Scintilla编辑组件构建的IDE。 Pydb是标准Python调试器pdb的一个版本,经过修改后可与DDD(数据显示调试器)一起使用,DDD是一种流行的图形化调试器前端。 Pydb可以在 <http://bashdb.sourceforge.net/pydb/> 找到,DDD可以在 <https://www.gnu.org/software/ddd> 找到。 有许多商业Python IDE包括图形调试器。他们包括: - Wing IDE (<https://wingware.com/>) - Komodo IDE (<https://komodoide.com/>) - PyCharm (<https://www.jetbrains.com/pycharm/>) ### [有没有工具来帮助找寻漏洞或进行静态分析?](#id5) 有的。 PyChecker 是一个寻找Python代码漏洞以及对代码复杂性和风格给出警告的工具。你可以从这里获得PyChecker: <http://pychecker.sourceforge.net/> 。 [Pylint](https://www.pylint.org/) \[https://www.pylint.org/\] 是另一个检查模块是否满足编码标准的工具,也可以编写插件来添加自定义功能。除了PyChecker 执行的错误检查之外, Pylint 还提供了一些额外的功能,例如检查行长度,变量名称是否根据您的编码标准格式良好,声明的接口是否完全实现等等。 <https://docs.pylint.org/> 提供了Pylint功能的完整列表。 静态类型检查器,例如 [Mypy](http://mypy-lang.org/) \[http://mypy-lang.org/\] 、 [Pyre](https://pyre-check.org/) \[https://pyre-check.org/\] 和 [Pytype](https://github.com/google/pytype) \[https://github.com/google/pytype\] 可以检查Python源代码中的类型提示。 ### [我如何能够通过一个 Python 脚本创建一个独立运行的二进制文件?](#id6) 如果你想要的只是一个独立的程序,用户可以下载和运行而不必先安装Python发行版,你就不需要将Python编译成C代码。有许多工具可以确定程序所需的模块集,并将这些模块与Python二进制文件绑定在一起以生成单个可执行文件。 一种是使用冻结工具,它包含在Python源代码树 `Tools/freeze` 中。它将Python字节代码转换为C数组;一个C编译器,你可以将所有模块嵌入到一个新程序中,然后将其与标准Python模块链接。 它的工作原理是递归扫描源代码以获取import语句(两种形式),并在标准Python路径和源目录(用于内置模块)中查找模块。 然后,它将用Python编写的模块的字节码转换为C代码(可以使用编组模块转换为代码对象的数组初始化器),并创建一个定制的配置文件,该文件仅包含程序中实际使用的内置模块。 然后,它编译生成的C代码并将其与Python解释器的其余部分链接,以形成一个独立的二进制文件,其行为与你的脚本完全相同。 显然, freeze 需要一个C编译器。有几个其他实用工具不需要。 一个是Thomas Heller的py2exe(仅限Windows) > <http://www.py2exe.org/> 另一个工具是 Anthony Tuininga 的 [cx\_Freeze](https://anthony-tuininga.github.io/cx_Freeze/) \[https://anthony-tuininga.github.io/cx\_Freeze/\]。 ### [是否有编程标准或Python程序的样式指南?](#id7) 是。标准库模块所要求的编码样式文档为 [**PEP 8**](https://www.python.org/dev/peps/pep-0008) \[https://www.python.org/dev/peps/pep-0008\] 。 ## [核心语言](#id8) ### [当变量有值时,为什么会出现UnboundLocalError?](#id9) 通过在函数体中的某处添加赋值语句,导致以前正常工作的代码被修改而得到 UnboundLocalError 会令人感到意外。 以下代码: ``` >>> x = 10 >>> def bar(): ... print(x) >>> bar() 10 ``` 正常工作,但是以下代码 ``` >>> x = 10 >>> def foo(): ... print(x) ... x += 1 ``` 会得到一个 UnboundLocalError : ``` >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment ``` 这是因为当你对作用域中的变量进行赋值时,该变量将成为该作用域的局部变量,并在外部作用域中隐藏任何类似命名的变量。由于foo中的最后一个语句为 `x` 分配了一个新值,编译器会将其识别为局部变量。因此,当先前的 `print(x)` 尝试打印未初始化的局部变量时会导致错误。 在上面的示例中,你可以通过将其声明为全局来访问外部作用域变量: ``` >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 >>> foobar() 10 ``` 这个显式声明是必需的,以便提醒你(与类和实例变量的表面类似情况不同),你实际上是在外部作用域中修改变量的值 ``` >>> print(x) 11 ``` 你可以使用 [`nonlocal`](../reference/simple_stmts.xhtml#nonlocal) 关键字在嵌套作用域中执行类似的操作: ``` >>> def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) >>> foo() 10 11 ``` ### [Python中的局部变量和全局变量有哪些规则?](#id10) 在Python中,仅在函数内引用的变量是隐式全局变量。如果在函数体内的任何位置为变量赋值,则除非明确声明为全局,否则将其视为局部值。 虽然起初有点令人惊讶,但片刻考虑就可以解释。一方面,要求 [`global`](../reference/simple_stmts.xhtml#global) 表示已分配的变量可以防止意外的副作用。另一方面,如果所有全局引用都需要 `global` ,那么你一直都在使用 `global` 。你必须将对内置函数或导入模块的组件的每个引用声明为全局。这种杂乱会破坏 `global` 声明用于识别副作用的有用性。 ### [为什么在具有不同值的循环中定义的lambdas都返回相同的结果?](#id11) 假设你使用for循环来定义几个不同的 lambda (甚至是普通函数),例如:: ``` >>> squares = [] >>> for x in range(5): ... squares.append(lambda: x**2) ``` 这给你一个包含5个lambdas的列表,它们计算 `x**2` 。你可能会期望,当它们被调用时,它们将分别返回 `0` 、 `1` 、 `4` 、 `9` 和 `16` 。但是,当你真正尝试时,你会看到它们都返回 `16` 。: ``` >>> squares[2]() 16 >>> squares[4]() 16 ``` 发生这种情况是因为 `x` 不是lambdas的内部变量,而是在外部作用域中定义,并且在调用lambda时访问它 - 而不是在定义它时。 在循环结束时, `x` 的值是 `4` ,所以所有的函数现在返回 `4**2` ,即 `16` 。你还可以通过更改 `x` 的值来验证这一点,并查看lambdas的结果如何变化: ``` >>> x = 8 >>> squares[2]() 64 ``` 为了避免这种情况,你需要将值保存在lambdas的局部变量中,这样它们就不依赖于全局``x`` 的值 ``` >>> squares = [] >>> for x in range(5): ... squares.append(lambda n=x: n**2) ``` 这里, `n=x` 在lambda本地创建一个新的变量 `n` ,并在定义lambda时计算,使它具有与 `x` 在循环中该点相同的值。这意味着 `n` 的值在第一个lambda中为 `0` ,在第二个lambda中为 `1` ,在第三个中为 `2` ,依此类推。因此每个lambda现在将返回正确的结果: ``` >>> squares[2]() 4 >>> squares[4]() 16 ``` 请注意,这种行为并不是lambda所特有的,但也适用于常规函数。 ### [如何跨模块共享全局变量?](#id12) 在单个程序中跨模块共享信息的规范方法是创建一个特殊模块(通常称为config或cfg)。只需在应用程序的所有模块中导入配置模块;然后该模块可用作全局名称。因为每个模块只有一个实例,所以对模块对象所做的任何更改都会在任何地方反映出来。 例如: config.py: ``` x = 0 # Default value of the 'x' configuration setting ``` mod.py: ``` import config config.x = 1 ``` main.py: ``` import config import mod print(config.x) ``` 请注意,出于同样的原因,使用模块也是实现Singleton设计模式的基础。 ### [导入模块的“最佳实践”是什么?](#id13) 通常,不要使用 `from modulename import *` 。这样做会使导入器的命名空间变得混乱,并且使得连接器更难以检测未定义的名称。 在文件的顶部导入模块。这样做可以清楚地了解代码所需的其他模块,并避免了模块名称是否在范围内的问题。每行导入一个模块可以轻松添加和删除导入的模块,但每行导入多个模块会占用更少的屏幕空间。 如果按以下顺序导入模块,这是一种很好的做法: 1. 标准库模块 -- 例如: `sys`, `os`, `getopt`, `re` 2. 第三方库模块(安装在Python的site-packages目录中的任何内容) -- 例如mx.DateTime,ZODB,PIL.Image等 3. 本地开发的模块 有时需要将模块导入语句移动到函数或类里面,以避免循环导入问题。Gordon McMillan 说: > 当两个模块都使用 "import <module>" 的导入形式时,循环导入就可以了。但是当第二个模块想从第一个模块中获取一个名称(“来自模块导入名称”)并且导入位于顶层时,就会出错。这是因为第一个模块中的名称还不可用,因为第一个模块正在忙着导入第二个模块。 在这种情况下,如果第二个模块仅用于一个函数,则可以轻松地将模块导入语句移动到该函数中。调用导入时,第一个模块将完成初始化,第二个模块可以进行导入。 如果某些模块是特定于平台的,则可能还需要将模块导入语句移出顶级代码。在这种情况下,甚至可能无法导入文件顶部的所有模块。在这种情况下,在相应的特定于平台的代码中导入正确的模块是一个很好的选择。 只有当需要解决诸如避免循环导入或试图减少模块初始化时间的问题时,才可以将导入移动到本地范围,例如在函数定义中。如果根据程序的执行方式,许多导入是不必要的,这种技术尤其有用。如果仅在某个函数中使用模块,您还可能希望将导入移到该函数中。请注意,第一次加载模块可能会因为模块的一次初始化而代价高昂,但多次加载模块实际上是免费的,只需进行几次字典查找。即使模块名称超出了作用域,模块也可能在 [`sys.modules`](../library/sys.xhtml#sys.modules "sys.modules") 中可用。 ### [为什么对象之间共享默认值?](#id14) 这种类型的缺陷通常会惹恼新手程序员。考虑这个函数 ``` def foo(mydict={}): # Danger: shared reference to one dict for all calls ... compute something ... mydict[key] = value return mydict ``` 第一次调用此函数时,`mydict` 包含一个项目。第二次,`mydict``包含两个项目,因为当 ``foo()` 开始执行时, `mydict` 已有一个项目。 It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined. If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object. By definition, immutable objects such as numbers, strings, tuples, and `None`, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion. Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use `None` as the default value and inside the function, check if the parameter is `None` and create a new list/dictionary/whatever if it is. For example, don't write: ``` def foo(mydict={}): ... ``` 但是: ``` def foo(mydict=None): if mydict is None: mydict = {} # create a new dict for local namespace ``` This feature can be useful. When you have a function that's time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called "memoizing", and can be implemented like this: ``` # Callers can only provide two parameters and optionally pass _cache by keyword def expensive(arg1, arg2, *, _cache={}): if (arg1, arg2) in _cache: return _cache[(arg1, arg2)] # Calculate the value result = ... expensive computation ... _cache[(arg1, arg2)] = result # Store result in the cache return result ``` You could use a global variable containing a dictionary instead of the default value; it's a matter of taste. ### [如何将可选参数或关键字参数从一个函数传递到另一个函数?](#id15) 使用函数参数列表中的 `*` 和 `**` 说明符收集参数;这会将位置参数作为元组,将关键字参数作为字典。然后,您可以使用 `*` 和 `**` 调用另一个函数时传递这些参数: ``` def f(x, *args, **kwargs): ... kwargs['width'] = '14.3c' ... g(x, *args, **kwargs) ``` ### [形参和实参之间有什么区别?](#id16) [Parameters](../glossary.xhtml#term-parameter) are defined by the names that appear in a function definition, whereas [arguments](../glossary.xhtml#term-argument) are the values actually passed to a function when calling it. Parameters define what types of arguments a function can accept. For example, given the function definition: ``` def func(foo, bar=None, **kwargs): pass ``` *foo*, *bar* 和 *kwargs* 是 `func` 的参数。但是,在调用 `func` 时,例如: ``` func(42, bar=314, extra=somevar) ``` the values `42`, `314`, and `somevar` are arguments. ### [为什么更改列表'y'也会更改列表'x'?](#id17) 如果你编写的代码就像下面一样: ``` >>> x = [] >>> y = x >>> y.append(10) >>> y [10] >>> x [10] ``` 你可能想知道为什么追加一个元素也改变了x。 产生这种结果有两个因素: 1. Variables are simply names that refer to objects. Doing `y = x` doesn't create a copy of the list -- it creates a new variable `y` that refers to the same object `x` refers to. This means that there is only one object (the list), and both `x` and `y` refer to it. 2. Lists are [mutable](../glossary.xhtml#term-mutable), which means that you can change their content. After the call to `append()`, the content of the mutable object has changed from `[]` to `[10]`. Since both the variables refer to the same object, using either name accesses the modified value `[10]`. 如果我们改为将不可变对象分配给 `x`: ``` >>> x = 5 # ints are immutable >>> y = x >>> x = x + 1 # 5 can't be mutated, we are creating a new object here >>> x 6 >>> y 5 ``` we can see that in this case `x` and `y` are not equal anymore. This is because integers are [immutable](../glossary.xhtml#term-immutable), and when we do `x = x + 1` we are not mutating the int `5` by incrementing its value; instead, we are creating a new object (the int `6`) and assigning it to `x` (that is, changing which object `x` refers to). After this assignment we have two objects (the ints `6` and `5`) and two variables that refer to them (`x` now refers to `6` but `y` still refers to `5`). Some operations (for example `y.append(10)` and `y.sort()`) mutate the object, whereas superficially similar operations (for example `y = y + [10]`and `sorted(y)`) create a new object. In general in Python (and in all cases in the standard library) a method that mutates an object will return `None`to help avoid getting the two types of operations confused. So if you mistakenly write `y.sort()` thinking it will give you a sorted copy of `y`, you'll instead end up with `None`, which will likely cause your program to generate an easily diagnosed error. However, there is one class of operations where the same operation sometimes has different behaviors with different types: the augmented assignment operators. For example, `+=` mutates lists but not tuples or ints ( ``` a_list += [1, 2, 3] ``` is equivalent to `a_list.extend([1, 2, 3])` and mutates `a_list`, whereas `some_tuple += (1, 2, 3)` and `some_int += 1` create new objects). 换而言之: - If we have a mutable object ([`list`](../library/stdtypes.xhtml#list "list"), [`dict`](../library/stdtypes.xhtml#dict "dict"), [`set`](../library/stdtypes.xhtml#set "set"), etc.), we can use some specific operations to mutate it and all the variables that refer to it will see the change. - If we have an immutable object ([`str`](../library/stdtypes.xhtml#str "str"), [`int`](../library/functions.xhtml#int "int"), [`tuple`](../library/stdtypes.xhtml#tuple "tuple"), etc.), all the variables that refer to it will always see the same value, but operations that transform that value into a new value always return a new object. If you want to know if two variables refer to the same object or not, you can use the [`is`](../reference/expressions.xhtml#is) operator, or the built-in function [`id()`](../library/functions.xhtml#id "id"). ### [如何编写带输出参数的函数(通过引用调用)?](#id18) Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there's no alias between an argument name in the caller and callee, and so no call-by-reference per se. You can achieve the desired effect in a number of ways. 1. By returning a tuple of the results: ``` def func2(a, b): a = 'new-value' # a and b are local names b = b + 1 # assigned to new objects return a, b # return new values x, y = 'old-value', 99 x, y = func2(x, y) print(x, y) # output: new-value 100 ``` This is almost always the clearest solution. 2. By using global variables. This isn't thread-safe, and is not recommended. 3. By passing a mutable (changeable in-place) object: ``` def func1(a): a[0] = 'new-value' # 'a' references a mutable list a[1] = a[1] + 1 # changes a shared object args = ['old-value', 99] func1(args) print(args[0], args[1]) # output: new-value 100 ``` 4. By passing in a dictionary that gets mutated: ``` def func3(args): args['a'] = 'new-value' # args is a mutable dictionary args['b'] = args['b'] + 1 # change it in-place args = {'a': 'old-value', 'b': 99} func3(args) print(args['a'], args['b']) ``` 5. Or bundle up values in a class instance: ``` class callByRef: def __init__(self, **args): for (key, value) in args.items(): setattr(self, key, value) def func4(args): args.a = 'new-value' # args is a mutable callByRef args.b = args.b + 1 # change object in-place args = callByRef(a='old-value', b=99) func4(args) print(args.a, args.b) ``` There's almost never a good reason to get this complicated. Your best choice is to return a tuple containing the multiple results. ### [如何在Python中创建高阶函数?](#id19) You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define `linear(a,b)` which returns a function `f(x)` that computes the value `a*x+b`. Using nested scopes: ``` def linear(a, b): def result(x): return a * x + b return result ``` Or using a callable object: ``` class linear: def __init__(self, a, b): self.a, self.b = a, b def __call__(self, x): return self.a * x + self.b ``` In both cases, ``` taxes = linear(0.3, 2) ``` gives a callable object where `taxes(10e6) == 0.3 * 10e6 + 2`. The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance: ``` class exponential(linear): # __init__ inherited def __call__(self, x): return self.a * (x ** self.b) ``` Object can encapsulate state for several methods: ``` class counter: value = 0 def set(self, x): self.value = x def up(self): self.value = self.value + 1 def down(self): self.value = self.value - 1 count = counter() inc, dec, reset = count.up, count.down, count.set ``` Here `inc()`, `dec()` and `reset()` act like functions which share the same counting variable. ### [如何在Python中复制对象?](#id20) In general, try [`copy.copy()`](../library/copy.xhtml#copy.copy "copy.copy") or [`copy.deepcopy()`](../library/copy.xhtml#copy.deepcopy "copy.deepcopy") for the general case. Not all objects can be copied, but most can. Some objects can be copied more easily. Dictionaries have a [`copy()`](../library/stdtypes.xhtml#dict.copy "dict.copy")method: ``` newdict = olddict.copy() ``` Sequences can be copied by slicing: ``` new_l = l[:] ``` ### [如何找到对象的方法或属性?](#id21) For an instance x of a user-defined class, `dir(x)` returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class. ### [我的代码如何才能发现对象的名称?](#id22) Generally speaking, it can't, because objects don't really have names. Essentially, assignment always binds a name to a value; The same is true of `def` and `class` statements, but in that case the value is a callable. Consider the following code: ``` >>> class A: ... pass ... >>> B = A >>> a = B() >>> b = a >>> print(b) <__main__.A object at 0x16D07CC> >>> print(a) <__main__.A object at 0x16D07CC> ``` Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance's name is a or b, since both names are bound to the same value. Generally speaking it should not be necessary for your code to "know the names" of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial. In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question: > The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn't really care -- so the only way to find out what it's called is to ask all your neighbours (namespaces) if it's their cat (object)... > > ....and don't be surprised if you'll find that it's known by many names, or no name at all! ### [逗号运算符的优先级是什么?](#id23) Comma is not an operator in Python. Consider this session: ``` >>> "a" in "b", "a" (False, 'a') ``` Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered: ``` ("a" in "b"), "a" ``` not: ``` "a" in ("b", "a") ``` The same is true of the various assignment operators (`=`, `+=` etc). They are not truly operators but syntactic delimiters in assignment statements. ### [Is there an equivalent of C's "?:" ternary operator?](#id24) Yes, there is. The syntax is as follows: ``` [on_true] if [expression] else [on_false] x, y = 50, 25 small = x if x < y else y ``` Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators: ``` [expression] and [on_true] or [on_false] ``` However, this idiom is unsafe, as it can give wrong results when *on\_true*has a false boolean value. Therefore, it is always better to use the `... if ... else ...` form. ### [Is it possible to write obfuscated one-liners in Python?](#id25) Yes. Usually this is done by nesting [`lambda`](../reference/expressions.xhtml#lambda) within `lambda`. See the following three examples, due to Ulf Bartelt: ``` from functools import reduce # Primes < 1000 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0, map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000))))) # First 10 Fibonacci numbers print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), range(10)))) # Mandelbrot set print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y, Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM, Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro, i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr( 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)) # \___ ___/ \___ ___/ | | |__ lines on screen # V V | |______ columns on screen # | | |__________ maximum of "iterations" # | |_________________ range on y axis # |____________________________ range on x axis ``` Don't try this at home, kids! ### [函数参数列表中的斜杠(/)是什么意思?](#id26) A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally-usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position. For example, [`pow()`](../library/functions.xhtml#pow "pow") is a function that accepts positional-only parameters. Its documentation looks like this: ``` >>> help(pow) Help on built-in function pow in module builtins: pow(x, y, z=None, /) Equivalent to x**y (with two arguments) or x**y % z (with three arguments) Some types, such as ints, are able to use a more efficient algorithm when invoked using the three argument form. ``` The slash at the end of the parameter list means that all three parameters are positional-only. Thus, calling [`pow()`](../library/functions.xhtml#pow "pow") with keyword aguments would lead to an error: ``` >>> pow(x=3, y=4) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: pow() takes no keyword arguments ``` Note that as of this writing this is only documentational and no valid syntax in Python, although there is [**PEP 570**](https://www.python.org/dev/peps/pep-0570) \[https://www.python.org/dev/peps/pep-0570\], which proposes a syntax for position-only parameters in Python. ## [数字和字符串](#id27) ### [如何指定十六进制和八进制整数?](#id28) To specify an octal digit, precede the octal value with a zero, and then a lower or uppercase "o". For example, to set the variable "a" to the octal value "10" (8 in decimal), type: ``` >>> a = 0o10 >>> a 8 ``` Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase "x". Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter: ``` >>> a = 0xa5 >>> a 165 >>> b = 0XB2 >>> b 178 ``` ### [为什么-22 // 10返回-3?](#id29) It's primarily driven by the desire that `i % j` have the same sign as `j`. If you want that, and also want: ``` i == (i // j) * j + (i % j) ``` then integer division has to return the floor. C also requires that identity to hold, and then compilers that truncate `i // j` need to make `i % j` have the same sign as `i`. There are few real use cases for `i % j` when `j` is negative. When `j`is positive, there are many, and in virtually all of them it's more useful for `i % j` to be `>= 0`. If the clock says 10 now, what did it say 200 hours ago? `-190 % 12 == 2` is useful; `-190 % 12 == -10` is a bug waiting to bite. ### [如何将字符串转换为数字?](#id30) For integers, use the built-in [`int()`](../library/functions.xhtml#int "int") type constructor, e.g. ``` int('144') == 144 ``` . Similarly, [`float()`](../library/functions.xhtml#float "float") converts to floating-point, e.g. `float('144') == 144.0`. By default, these interpret the number as decimal, so that ``` int('0144') == 144 ``` and `int('0x144')` raises [`ValueError`](../library/exceptions.xhtml#ValueError "ValueError"). `int(string, base)` takes the base to convert from as a second optional argument, so ``` int('0x144', 16) == 324 ``` . If the base is specified as 0, the number is interpreted using Python's rules: a leading '0o' indicates octal, and '0x' indicates a hex number. Do not use the built-in function [`eval()`](../library/functions.xhtml#eval "eval") if all you need is to convert strings to numbers. [`eval()`](../library/functions.xhtml#eval "eval") will be significantly slower and it presents a security risk: someone could pass you a Python expression that might have unwanted side effects. For example, someone could pass `__import__('os').system("rm -rf $HOME")` which would erase your home directory. [`eval()`](../library/functions.xhtml#eval "eval") also has the effect of interpreting numbers as Python expressions, so that e.g. `eval('09')` gives a syntax error because Python does not allow leading '0' in a decimal number (except '0'). ### [如何将数字转换为字符串?](#id31) To convert, e.g., the number 144 to the string '144', use the built-in type constructor [`str()`](../library/stdtypes.xhtml#str "str"). If you want a hexadecimal or octal representation, use the built-in functions [`hex()`](../library/functions.xhtml#hex "hex") or [`oct()`](../library/functions.xhtml#oct "oct"). For fancy formatting, see the [格式化字符串字面值](../reference/lexical_analysis.xhtml#f-strings) and [格式字符串语法](../library/string.xhtml#formatstrings) sections, e.g. `"{:04d}".format(144)` yields `'0144'` and `"{:.3f}".format(1.0/3.0)` yields `'0.333'`. ### [如何修改字符串?](#id32) You can't, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-place unicode data, try using an [`io.StringIO`](../library/io.xhtml#io.StringIO "io.StringIO") object or the [`array`](../library/array.xhtml#module-array "array: Space efficient arrays of uniformly typed numeric values.")module: ``` >>> import io >>> s = "Hello, world" >>> sio = io.StringIO(s) >>> sio.getvalue() 'Hello, world' >>> sio.seek(7) 7 >>> sio.write("there!") 6 >>> sio.getvalue() 'Hello, there!' >>> import array >>> a = array.array('u', s) >>> print(a) array('u', 'Hello, world') >>> a[0] = 'y' >>> print(a) array('u', 'yello, world') >>> a.tounicode() 'yello, world' ``` ### [如何使用字符串调用函数/方法?](#id33) 有各种技术。 - The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct: ``` def a(): pass def b(): pass dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs dispatch[get_input()]() # Note trailing parens to call function ``` - Use the built-in function [`getattr()`](../library/functions.xhtml#getattr "getattr"): ``` import foo getattr(foo, 'bar')() ``` Note that [`getattr()`](../library/functions.xhtml#getattr "getattr") works on any object, including classes, class instances, modules, and so on. This is used in several places in the standard library, like this: ``` class Foo: def do_foo(self): ... def do_bar(self): ... f = getattr(foo_instance, 'do_' + opname) f() ``` - Use [`locals()`](../library/functions.xhtml#locals "locals") or [`eval()`](../library/functions.xhtml#eval "eval") to resolve the function name: ``` def myFunc(): print("hello") fname = "myFunc" f = locals()[fname] f() f = eval(fname) f() ``` Note: Using [`eval()`](../library/functions.xhtml#eval "eval") is slow and dangerous. If you don't have absolute control over the contents of the string, someone could pass a string that resulted in an arbitrary function being executed. ### [Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?](#id34) You can use `S.rstrip("\r\n")` to remove all occurrences of any line terminator from the end of the string `S` without removing other trailing whitespace. If the string `S` represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed: ``` >>> lines = ("line 1 \r\n" ... "\r\n" ... "\r\n") >>> lines.rstrip("\n\r") 'line 1 ' ``` Since this is typically only desired when reading text one line at a time, using `S.rstrip()` this way works well. ### [Is there a scanf() or sscanf() equivalent?](#id35) Not as such. For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the [`split()`](../library/stdtypes.xhtml#str.split "str.split") method of string objects and then convert decimal strings to numeric values using [`int()`](../library/functions.xhtml#int "int") or [`float()`](../library/functions.xhtml#float "float"). `split()` supports an optional "sep" parameter which is useful if the line uses something other than whitespace as a separator. For more complicated input parsing, regular expressions are more powerful than C's `sscanf()` and better suited for the task. ### [What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?](#id36) 见 [Unicode 指南](../howto/unicode.xhtml#unicode-howto) ## [性能](#id37) ### [我的程序太慢了。该如何加快速度?](#id38) That's a tough one, in general. First, here are a list of things to remember before diving further: - Performance characteristics vary across Python implementations. This FAQ focusses on [CPython](../glossary.xhtml#term-cpython). - Behaviour can vary across operating systems, especially when talking about I/O or multi-threading. - You should always find the hot spots in your program *before* attempting to optimize any code (see the [`profile`](../library/profile.xhtml#module-profile "profile: Python source profiler.") module). - Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the [`timeit`](../library/timeit.xhtml#module-timeit "timeit: Measure the execution time of small code snippets.") module). - It is highly recommended to have good code coverage (through unit testing or any other technique) before potentially introducing regressions hidden in sophisticated optimizations. That being said, there are many tricks to speed up Python code. Here are some general principles which go a long way towards reaching acceptable performance levels: - Making your algorithms faster (or changing to faster ones) can yield much larger benefits than trying to sprinkle micro-optimization tricks all over your code. - Use the right data structures. Study documentation for the [内置类型](../library/stdtypes.xhtml#bltin-types)and the [`collections`](../library/collections.xhtml#module-collections "collections: Container datatypes") module. - When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the [`list.sort()`](../library/stdtypes.xhtml#list.sort "list.sort") built-in method or the related [`sorted()`](../library/functions.xhtml#sorted "sorted")function to do sorting (and see the [排序指南](../howto/sorting.xhtml#sortinghowto) for examples of moderately advanced usage). - Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability). If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, [Cython](http://cython.org) \[http://cython.org\] can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also [write a C extension module](../extending/index.xhtml#extending-index)yourself. 参见 The wiki page devoted to [performance tips](https://wiki.python.org/moin/PythonSpeed/PerformanceTips) \[https://wiki.python.org/moin/PythonSpeed/PerformanceTips\]. ### [将多个字符串连接在一起的最有效方法是什么?](#id39) [`str`](../library/stdtypes.xhtml#str "str") and [`bytes`](../library/stdtypes.xhtml#bytes "bytes") objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length. To accumulate many [`str`](../library/stdtypes.xhtml#str "str") objects, the recommended idiom is to place them into a list and call [`str.join()`](../library/stdtypes.xhtml#str.join "str.join") at the end: ``` chunks = [] for s in my_strings: chunks.append(s) result = ''.join(chunks) ``` (another reasonably efficient idiom is to use [`io.StringIO`](../library/io.xhtml#io.StringIO "io.StringIO")) To accumulate many [`bytes`](../library/stdtypes.xhtml#bytes "bytes") objects, the recommended idiom is to extend a [`bytearray`](../library/stdtypes.xhtml#bytearray "bytearray") object using in-place concatenation (the `+=` operator): ``` result = bytearray() for b in my_bytes_objects: result += b ``` ## [序列(元组/列表)](#id40) ### [如何在元组和列表之间进行转换?](#id41) The type constructor `tuple(seq)` converts any sequence (actually, any iterable) into a tuple with the same items in the same order. For example, `tuple([1, 2, 3])` yields `(1, 2, 3)` and `tuple('abc')`yields `('a', 'b', 'c')`. If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call [`tuple()`](../library/stdtypes.xhtml#tuple "tuple") when you aren't sure that an object is already a tuple. The type constructor `list(seq)` converts any sequence or iterable into a list with the same items in the same order. For example, `list((1, 2, 3))` yields `[1, 2, 3]` and `list('abc')` yields `['a', 'b', 'c']`. If the argument is a list, it makes a copy just like `seq[:]` would. ### [什么是负数序号?](#id42) Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of `seq[-n]` as the same as `seq[len(seq)-n]`. Using negative indices can be very convenient. For example `S[:-1]` is all of the string except for its last character, which is useful for removing the trailing newline from a string. ### [如何以相反的顺序迭代序列?](#id43) Use the [`reversed()`](../library/functions.xhtml#reversed "reversed") built-in function, which is new in Python 2.4: ``` for x in reversed(sequence): ... # do something with x ... ``` This won't touch your original sequence, but build a new copy with reversed order to iterate over. With Python 2.3, you can use an extended slice syntax: ``` for x in sequence[::-1]: ... # do something with x ... ``` ### [如何从列表中删除重复项?](#id44) See the Python Cookbook for a long discussion of many ways to do this: > <https://code.activestate.com/recipes/52560/> If you don't mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go: ``` if mylist: mylist.sort() last = mylist[-1] for i in range(len(mylist)-2, -1, -1): if last == mylist[i]: del mylist[i] else: last = mylist[i] ``` If all elements of the list may be used as set keys (i.e. they are all [hashable](../glossary.xhtml#term-hashable)) this is often faster ``` mylist = list(set(mylist)) ``` This converts the list into a set, thereby removing duplicates, and then back into a list. ### [你如何在Python中创建一个数组?](#id45) 使用列表: ``` ["this", 1, "is", "an", "array"] ``` Lists are equivalent to C or Pascal arrays in their time complexity; the primary difference is that a Python list can contain objects of many different types. The `array` module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that the Numeric extensions and others define array-like structures with various characteristics as well. To get Lisp-style linked lists, you can emulate cons cells using tuples: ``` lisp_list = ("like", ("this", ("example", None) ) ) ``` If mutability is desired, you could use lists instead of tuples. Here the analogue of lisp car is `lisp_list[0]` and the analogue of cdr is `lisp_list[1]`. Only do this if you're sure you really need to, because it's usually a lot slower than using Python lists. ### [如何创建多维列表?](#id46) 你可能试图制作一个像这样的多维数组: ``` >>> A = [[None] * 2] * 3 ``` 如果你打印它,看起来是正确的: ``` >>> A [[None, None], [None, None], [None, None]] ``` 但是,当您分配值时,它会显示在多个位置: ``` >>> A[0][0] = 5 >>> A [[5, None], [5, None], [5, None]] ``` The reason is that replicating a list with `*` doesn't create copies, it only creates references to the existing objects. The `*3` creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want. The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list: ``` A = [None] * 3 for i in range(3): A[i] = [None] * 2 ``` This generates a list containing 3 different lists of length two. You can also use a list comprehension: ``` w, h = 2, 3 A = [[None] * w for i in range(h)] ``` Or, you can use an extension that provides a matrix datatype; [NumPy](http://www.numpy.org/) \[http://www.numpy.org/\] is the best known. ### [如何将方法应用于一系列对象?](#id47) Use a list comprehension: ``` result = [obj.method() for obj in mylist] ``` ### [Why does a\_tuple\[i\] += \['item'\] raise an exception when the addition works?](#id48) This is because of a combination of the fact that augmented assignment operators are *assignment* operators, and the difference between mutable and immutable objects in Python. This discussion applies in general when augmented assignment operators are applied to elements of a tuple that point to mutable objects, but we'll use a `list` and `+=` as our exemplar. If you wrote: ``` >>> a_tuple = (1, 2) >>> a_tuple[0] += 1 Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment ``` The reason for the exception should be immediately clear: `1` is added to the object `a_tuple[0]` points to (`1`), producing the result object, `2`, but when we attempt to assign the result of the computation, `2`, to element `0` of the tuple, we get an error because we can't change what an element of a tuple points to. Under the covers, what this augmented assignment statement is doing is approximately this: ``` >>> result = a_tuple[0] + 1 >>> a_tuple[0] = result Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment ``` It is the assignment part of the operation that produces the error, since a tuple is immutable. When you write something like: ``` >>> a_tuple = (['foo'], 'bar') >>> a_tuple[0] += ['item'] Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment ``` The exception is a bit more surprising, and even more surprising is the fact that even though there was an error, the append worked: ``` >>> a_tuple[0] ['foo', 'item'] ``` To see why this happens, you need to know that (a) if an object implements an `__iadd__` magic method, it gets called when the `+=` augmented assignment is executed, and its return value is what gets used in the assignment statement; and (b) for lists, `__iadd__` is equivalent to calling `extend` on the list and returning the list. That's why we say that for lists, `+=` is a "shorthand" for `list.extend`: ``` >>> a_list = [] >>> a_list += [1] >>> a_list [1] ``` This is equivalent to: ``` >>> result = a_list.__iadd__([1]) >>> a_list = result ``` The object pointed to by a\_list has been mutated, and the pointer to the mutated object is assigned back to `a_list`. The end result of the assignment is a no-op, since it is a pointer to the same object that `a_list`was previously pointing to, but the assignment still happens. Thus, in our tuple example what is happening is equivalent to: ``` >>> result = a_tuple[0].__iadd__(['item']) >>> a_tuple[0] = result Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment ``` The `__iadd__` succeeds, and thus the list is extended, but even though `result` points to the same object that `a_tuple[0]` already points to, that final assignment still results in an error, because tuples are immutable. ### [I want to do a complicated sort: can you do a Schwartzian Transform in Python?](#id49) The technique, attributed to Randal Schwartz of the Perl community, sorts the elements of a list by a metric which maps each element to its "sort value". In Python, use the `key` argument for the [`list.sort()`](../library/stdtypes.xhtml#list.sort "list.sort") method: ``` Isorted = L[:] Isorted.sort(key=lambda s: int(s[10:15])) ``` ### [如何按其他列表中的值对一个列表进行排序?](#id50) Merge them into an iterator of tuples, sort the resulting list, and then pick out the element you want. ``` >>> list1 = ["what", "I'm", "sorting", "by"] >>> list2 = ["something", "else", "to", "sort"] >>> pairs = zip(list1, list2) >>> pairs = sorted(pairs) >>> pairs [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')] >>> result = [x[1] for x in pairs] >>> result ['else', 'sort', 'to', 'something'] ``` An alternative for the last step is: ``` >>> result = [] >>> for p in pairs: result.append(p[1]) ``` If you find this more legible, you might prefer to use this instead of the final list comprehension. However, it is almost twice as slow for long lists. Why? First, the `append()` operation has to reallocate memory, and while it uses some tricks to avoid doing that each time, it still has to do it occasionally, and that costs quite a bit. Second, the expression "result.append" requires an extra attribute lookup, and third, there's a speed reduction from having to make all those function calls. ## [对象](#id51) ### [什么是类?](#id52) A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embody both the data (attributes) and code (methods) specific to a datatype. A class can be based on one or more other classes, called its base class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined by inheritance. You might have a generic `Mailbox` class that provides basic accessor methods for a mailbox, and subclasses such as `MboxMailbox`, `MaildirMailbox`, `OutlookMailbox`that handle various specific mailbox formats. ### [What is a method?](#id53) A method is a function on some object `x` that you normally call as `x.name(arguments...)`. Methods are defined as functions inside the class definition: ``` class C: def meth(self, arg): return arg * 2 + self.attribute ``` ### [什么是self?](#id54) Self is merely a conventional name for the first argument of a method. A method defined as `meth(self, a, b, c)` should be called as `x.meth(a, b, c)` for some instance `x` of the class in which the definition occurs; the called method will think it is called as `meth(x, a, b, c)`. 另请参阅 [为什么必须在方法定义和调用中显式使用“self”?](design.xhtml#why-self) 。 ### [如何检查对象是否是给定类或其子类的实例?](#id55) Use the built-in function `isinstance(obj, cls)`. You can check if an object is an instance of any of a number of classes by providing a tuple instead of a single class, e.g. `isinstance(obj, (class1, class2, ...))`, and can also check whether an object is one of Python's built-in types, e.g. `isinstance(obj, str)` or `isinstance(obj, (int, float, complex))`. Note that most programs do not use [`isinstance()`](../library/functions.xhtml#isinstance "isinstance") on user-defined classes very often. If you are developing the classes yourself, a more proper object-oriented style is to define methods on the classes that encapsulate a particular behaviour, instead of checking the object's class and doing a different thing based on what class it is. For example, if you have a function that does something: ``` def search(obj): if isinstance(obj, Mailbox): ... # code to search a mailbox elif isinstance(obj, Document): ... # code to search a document elif ... ``` A better approach is to define a `search()` method on all the classes and just call it: ``` class Mailbox: def search(self): ... # code to search a mailbox class Document: def search(self): ... # code to search a document obj.search() ``` ### [什么是代理?](#id56) Delegation is an object oriented technique (also called a design pattern). Let's say you have an object `x` and want to change the behaviour of just one of its methods. You can create a new class that provides a new implementation of the method you're interested in changing and delegates all other methods to the corresponding method of `x`. Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts all written data to uppercase: ``` class UpperOut: def __init__(self, outfile): self._outfile = outfile def write(self, s): self._outfile.write(s.upper()) def __getattr__(self, name): return getattr(self._outfile, name) ``` Here the `UpperOut` class redefines the `write()` method to convert the argument string to uppercase before calling the underlying `self.__outfile.write()` method. All other methods are delegated to the underlying `self.__outfile` object. The delegation is accomplished via the `__getattr__` method; consult [the language reference](../reference/datamodel.xhtml#attribute-access)for more information about controlling attribute access. Note that for more general cases delegation can get trickier. When attributes must be set as well as retrieved, the class must define a [`__setattr__()`](../reference/datamodel.xhtml#object.__setattr__ "object.__setattr__")method too, and it must do so carefully. The basic implementation of [`__setattr__()`](../reference/datamodel.xhtml#object.__setattr__ "object.__setattr__") is roughly equivalent to the following: ``` class X: ... def __setattr__(self, name, value): self.__dict__[name] = value ... ``` Most [`__setattr__()`](../reference/datamodel.xhtml#object.__setattr__ "object.__setattr__") implementations must modify `self.__dict__` to store local state for self without causing an infinite recursion. ### [如何从覆盖基类的派生类调用基类中定义的方法?](#id57) Use the built-in [`super()`](../library/functions.xhtml#super "super") function: ``` class Derived(Base): def meth(self): super(Derived, self).meth() ``` For version prior to 3.0, you may be using classic classes: For a class definition such as `class Derived(Base): ...` you can call method `meth()`defined in `Base` (or one of `Base`'s base classes) as ``` Base.meth(self, arguments...) ``` . Here, `Base.meth` is an unbound method, so you need to provide the `self` argument. ### [如何组织代码以便更改基类?](#id58) You could define an alias for the base class, assign the real base class to it before your class definition, and use the alias throughout your class. Then all you have to change is the value assigned to the alias. Incidentally, this trick is also handy if you want to decide dynamically (e.g. depending on availability of resources) which base class to use. Example: ``` BaseAlias = <real base class> class Derived(BaseAlias): def meth(self): BaseAlias.meth(self) ... ``` ### [如何创建静态类数据和静态类方法?](#id59) Both static data and static methods (in the sense of C++ or Java) are supported in Python. For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment: ``` class C: count = 0 # number of times C.__init__ called def __init__(self): C.count = C.count + 1 def getcount(self): return C.count # or return self.count ``` `c.count` also refers to `C.count` for any `c` such that ``` isinstance(c, C) ``` holds, unless overridden by `c` itself or by some class on the base-class search path from `c.__class__` back to `C`. Caution: within a method of C, an assignment like `self.count = 42` creates a new and unrelated instance named "count" in `self`'s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not: ``` C.count = 314 ``` Static methods are possible: ``` class C: @staticmethod def static(arg1, arg2, arg3): # No 'self' parameter! ... ``` However, a far more straightforward way to get the effect of a static method is via a simple module-level function: ``` def getcount(): return C.count ``` If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation. ### [如何在Python中重载构造函数(或方法)?](#id60) This answer actually applies to all methods, but the question usually comes up first in the context of constructors. In C++ you'd write ``` class C { C() { cout << "No arguments\n"; } C(int i) { cout << "Argument is " << i << "\n"; } } ``` In Python you have to write a single constructor that catches all cases using default arguments. For example: ``` class C: def __init__(self, i=None): if i is None: print("No arguments") else: print("Argument is", i) ``` This is not entirely equivalent, but close enough in practice. You could also try a variable-length argument list, e.g. ``` def __init__(self, *args): ... ``` The same approach works for all method definitions. ### [I try to use \_\_spam and I get an error about \_SomeClassName\_\_spam.](#id61) Variable names with double leading underscores are "mangled" to provide a simple but effective way to define class private variables. Any identifier of the form `__spam` (at least two leading underscores, at most one trailing underscore) is textually replaced with `_classname__spam`, where `classname` is the current class name with any leading underscores stripped. This doesn't guarantee privacy: an outside user can still deliberately access the "\_classname\_\_spam" attribute, and private values are visible in the object's `__dict__`. Many Python programmers never bother to use private variable names at all. ### [My class defines \_\_del\_\_ but it is not called when I delete the object.](#id62) 这有几个可能的原因。 The del statement does not necessarily call [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") -- it simply decrements the object's reference count, and if this reaches zero [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") is called. If your data structures contain circular links (e.g. a tree where each child has a parent reference and each parent has a list of children) the reference counts will never go back to zero. Once in a while Python runs an algorithm to detect such cycles, but the garbage collector might run some time after the last reference to your data structure vanishes, so your [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") method may be called at an inconvenient and random time. This is inconvenient if you're trying to reproduce a problem. Worse, the order in which object's [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__")methods are executed is arbitrary. You can run [`gc.collect()`](../library/gc.xhtml#gc.collect "gc.collect") to force a collection, but there *are* pathological cases where objects will never be collected. Despite the cycle collector, it's still a good idea to define an explicit `close()` method on objects to be called whenever you're done with them. The `close()` method can then remove attributes that refer to subobjects. Don't call [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") directly -- [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") should call `close()` and `close()` should make sure that it can be called more than once for the same object. Another way to avoid cyclical references is to use the [`weakref`](../library/weakref.xhtml#module-weakref "weakref: Support for weak references and weak dictionaries.") module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent and sibling references (if they need them!). Finally, if your [`__del__()`](../reference/datamodel.xhtml#object.__del__ "object.__del__") method raises an exception, a warning message is printed to [`sys.stderr`](../library/sys.xhtml#sys.stderr "sys.stderr"). ### [如何获取给定类的所有实例的列表?](#id63) Python does not keep track of all instances of a class (or of a built-in type). You can program the class's constructor to keep track of all instances by keeping a list of weak references to each instance. ### [为什么 `id()` 的结果看起来不是唯一的?](#id64) The [`id()`](../library/functions.xhtml#id "id") builtin returns an integer that is guaranteed to be unique during the lifetime of the object. Since in CPython, this is the object's memory address, it happens frequently that after an object is deleted from memory, the next freshly created object is allocated at the same position in memory. This is illustrated by this example: ``` >>> id(1000) # doctest: +SKIP 13901272 >>> id(2000) # doctest: +SKIP 13901272 ``` The two ids belong to different integer objects that are created before, and deleted immediately after execution of the `id()` call. To be sure that objects whose id you want to examine are still alive, create another reference to the object: ``` >>> a = 1000; b = 2000 >>> id(a) # doctest: +SKIP 13901272 >>> id(b) # doctest: +SKIP 13891296 ``` ## [模块](#id65) ### [怎么创建.pyc文件?](#id66) When a module is imported for the first time (or when the source file has changed since the current compiled file was created) a `.pyc` file containing the compiled code should be created in a `__pycache__` subdirectory of the directory containing the `.py` file. The `.pyc` file will have a filename that starts with the same name as the `.py` file, and ends with `.pyc`, with a middle component that depends on the particular `python`binary that created it. (See [**PEP 3147**](https://www.python.org/dev/peps/pep-3147) \[https://www.python.org/dev/peps/pep-3147\] for details.) One reason that a `.pyc` file may not be created is a permissions problem with the directory containing the source file, meaning that the `__pycache__`subdirectory cannot be created. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server. Unless the [`PYTHONDONTWRITEBYTECODE`](../using/cmdline.xhtml#envvar-PYTHONDONTWRITEBYTECODE) environment variable is set, creation of a .pyc file is automatic if you're importing a module and Python has the ability (permissions, free space, etc...) to create a `__pycache__`subdirectory and write the compiled module to that subdirectory. Running Python on a top level script is not considered an import and no `.pyc` will be created. For example, if you have a top-level module `foo.py` that imports another module `xyz.py`, when you run `foo` (by typing `python foo.py` as a shell command), a `.pyc` will be created for `xyz` because `xyz` is imported, but no `.pyc` file will be created for `foo` since `foo.py` isn't being imported. If you need to create a `.pyc` file for `foo` -- that is, to create a `.pyc` file for a module that is not imported -- you can, using the [`py_compile`](../library/py_compile.xhtml#module-py_compile "py_compile: Generate byte-code files from Python source files.") and [`compileall`](../library/compileall.xhtml#module-compileall "compileall: Tools for byte-compiling all Python source files in a directory tree.") modules. The [`py_compile`](../library/py_compile.xhtml#module-py_compile "py_compile: Generate byte-code files from Python source files.") module can manually compile any module. One way is to use the `compile()` function in that module interactively: ``` >>> import py_compile >>> py_compile.compile('foo.py') ``` This will write the `.pyc` to a `__pycache__` subdirectory in the same location as `foo.py` (or you can override that with the optional parameter `cfile`). You can also automatically compile all files in a directory or directories using the [`compileall`](../library/compileall.xhtml#module-compileall "compileall: Tools for byte-compiling all Python source files in a directory tree.") module. You can do it from the shell prompt by running `compileall.py` and providing the path of a directory containing Python files to compile: ``` python -m compileall . ``` ### [如何找到当前模块名称?](#id67) A module can find out its own module name by looking at the predefined global variable `__name__`. If this has the value `'__main__'`, the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking `__name__`: ``` def main(): print('Running test...') ... if __name__ == '__main__': main() ``` ### [怎样才能拥有相互导入的模块?](#id68) 假设您有以下模块: foo.py: ``` from bar import bar_var foo_var = 1 ``` bar.py: ``` from foo import foo_var bar_var = 2 ``` The problem is that the interpreter will perform the following steps: - main imports foo - Empty globals for foo are created - foo is compiled and starts executing - foo imports bar - Empty globals for bar are created - bar is compiled and starts executing - bar imports foo (which is a no-op since there already is a module named foo) - bar.foo\_var = foo.foo\_var The last step fails, because Python isn't done with interpreting `foo` yet and the global symbol dictionary for `foo` is still empty. The same thing happens when you use `import foo`, and then try to access `foo.foo_var` in global code. There are (at least) three possible workarounds for this problem. Guido van Rossum recommends avoiding all uses of `from <module> import ...`, and placing all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only. This means everything from an imported module is referenced as `<module>.<name>`. Jim Roskind建议在每个模块中按以下顺序执行步骤: - exports(全局变量,函数和不需要导入基类的类) - `import` statements - active code (including globals that are initialized from imported values). van Rossum doesn't like this approach much because the imports appear in a strange place, but it does work. Matthias Urlichs recommends restructuring your code so that the recursive import is not necessary in the first place. These solutions are not mutually exclusive. ### [\_\_import\_\_('x.y.z') returns <module 'x'>; 如何获取z?](#id69) Consider using the convenience function [`import_module()`](../library/importlib.xhtml#importlib.import_module "importlib.import_module") from [`importlib`](../library/importlib.xhtml#module-importlib "importlib: The implementation of the import machinery.") instead: ``` z = importlib.import_module('x.y.z') ``` ### [当我编辑导入的模块并重新导入它时,更改不会显示。为什么会这样?](#id70) 出于效率和一致性的原因,Python仅在第一次导入模块时读取模块文件。如果不这么做,在一个由许多模块组成的程序中,每个模块都会导入相同的基本模块,那么基本模块将被解析和重新解析多次。要强制重新读取已更改的模块,请执行以下操作: ``` import importlib import modname importlib.reload(modname) ``` 警告:这种技术不是100%万无一失。特别是包含如下语句的模块 ``` from modname import some_objects ``` 将继续使用旧版本的导入对象。如果模块包含类定义,则不会更新现有的类实例以使用新的类定义。这可能导致以下矛盾行为: ``` >>> import importlib >>> import cls >>> c = cls.C() # Create an instance of C >>> importlib.reload(cls) <module 'cls' from 'cls.py'> >>> isinstance(c, cls.C) # isinstance is false?!? False ``` 如果打印出类对象的“标识”,问题的本质就会明确: ``` >>> hex(id(c.__class__)) '0x7352a0' >>> hex(id(cls.C)) '0x4198d0' ``` ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](design.xhtml "设计和历史常见问题") | - [上一页](general.xhtml "Python常见问题") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 常见问题](index.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。