A function definition defines a user-defined function object (see section 3.2):
一个函数定义定义了一个用户自定义函数对象 (见3.2节).
funcdef |
::= | "def" funcname "(" [parameter_list] ")"
":" suite |
parameter_list |
::= | (defparameter ",")* |
("*" identifier [, "**" identifier] | ||
| "**" identifier
| defparameter [","]) | ||
defparameter |
::= | parameter ["=" expression] |
sublist |
::= | parameter ("," parameter)* [","] |
parameter |
::= | identifier | "(" sublist ")" |
funcname |
::= | identifier |
A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.
函数定义是一个可执行语句.它在当前局部名字空间中将函数名字与函数对象(函数的可执行代码的包装)捆绑在一起.这个函数对象包括着一个全局名字空间的引用, 以便在调用时使用.
The function definition does not execute the function body; this gets executed only when the function is called.
函数定义不执行函数体, 它们仅仅在调用时执行.
When one or more top-level parameters have the form parameter
=
expression, the function is said to have ``default
parameter values.'' For a parameter with a
default value, the corresponding argument may be omitted from a call,
in which case the parameter's default value is substituted. If a
parameter has a default value, all following parameters must also have
a default value -- this is a syntactic restriction that is not
expressed by the grammar.
当一个或多个参数以形式出现时, 这个函数就称为具有"默认参数值". 对于有默认参数值的参数, 在调用时它们可以省略, 此时他们被赋予默认值.如果某参数具有默认值, 则所有以后的参数都必须有默认值 -- 这是一个在上述语法说明中没有提到的限制.
Default parameter values are evaluated when the function
definition is executed. This means that the expression is evaluated
once, when the function is defined, and that that same
``pre-computed'' value is used for each call. This is especially
important to understand when a default parameter is a mutable object,
such as a list or a dictionary: if the function modifies the object
(e.g. by appending an item to a list), the default value is in effect
modified. This is generally not what was intended. A way around this
is to use None
as the default, and explicitly test for it in
the body of the function, e.g.:
默认参数值在函数定义被执行时计算.这意味着这个表达式仅仅求值一次, 时间是函数定义时, 在并且在每次调用时都使用相同的"预计算"的值.这在理解默认参数值是一个像列表,字典这样的可变对象时特别值得注意.如果修改了这个对象(例如给列表追加了一项).默认值也随之修改.这通常不是想要发生的.一个避免这个麻烦的方法就是使用None作默认值, 并且在函数体是作显式的测试, 例如:
def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin
Function call semantics are described in more detail in
section 5.3.4.
A function call always assigns values to all parameters mentioned in
the parameter list, either from position arguments, from keyword
arguments, or from default values. If the form ``*identifier
''
is present, it is initialized to a tuple receiving any excess
positional parameters, defaulting to the empty tuple. If the form
``**identifier
'' is present, it is initialized to a new
dictionary receiving any excess keyword arguments, defaulting to a
new empty dictionary.
函数调用语义的详细说明,参见5.3.4节. 通常一个函数调用会给所有出现在参数表中的参数赋一个值,要么是位置参数, 或者关键字参数, 或是默认值.如果使用的"*identifier"形式,它就被初始化一个接受所有额外位置参数的元组, 默认为空元组.如果使用的"**identifier"形式,它就被初始化一个接受所有额外关键字参数的字典, 默认为空元组.
It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda forms, described in section 5.11. Note that the lambda form is merely a shorthand for a simplified function definition; a function defined in a ``def'' statement can be passed around or assigned to another name just like a function defined by a lambda form. The ``def'' form is actually more powerful since it allows the execution of multiple statements.
也可以创建匿名函数(没和名字捆绑的函数), 能直接在表达式中使用.这是通过lambda表达式实现的,详见5.10节.注意lambda仅仅是一个简单函数的简写形式; 以"def"定义的函数可以传递, 可以赋予另一个名字, 就和以lambda定义的函数一样.以"def"定义的函数功能要更强大些, 因为它允许执行多条语句.
Programmer's note: Functions are first-class objects. A
``def
'' form executed inside a function definition defines a
local function that can be returned or passed around. Free variables
used in the nested function can access the local variables of the
function containing the def. See section 4.1 for details.
程序员注意: 在函数定义中执行的"def", 可以返回和传递.在嵌套函数中名字的语义会在Python 2.2中有所改变,可以参考附录中对新语义的描述.