python函数总结

8.6. Function definitions

A function definition defines a user-defined function object (see section The standard type hierarchy):

一个函数定义,定义一个用户自定义的函数对象

funcdef                 ::=  [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite
decorators              ::=  decorator+
decorator               ::=  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
dotted_name             ::=  identifier ("." identifier)*
parameter_list          ::=  defparameter ("," defparameter)* ["," [parameter_list_starargs]]
                             | parameter_list_starargs
parameter_list_starargs ::=  "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
                             | "**" parameter [","]
parameter               ::=  identifier [":" expression]
defparameter            ::=  parameter ["=" expression]
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). 

函数的执行绑定当前命名空间中的函数名和一个函数对象(一个围绕该函数可执行代码的包装器)

----------------------------------上述逻辑行的解释--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Logical lines逻辑行

The end of a logical line is represented by the token NEWLINE. 

逻辑行的结束由NEWlINE标记表示

Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). 

语句不能跨越逻辑行边界,除非NEWLINE被语法允许(e.g:复合语句中的语句之间)

A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

一个逻辑行,在显示或隐式的规则约束下,由一个或多个物理行构成

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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. [3]

函数定义不会执行函数体,只有当函数被调用时才会执行

A function definition may be wrapped by one or more decorator expressions. 

一个函数定义可能被一个或多个装饰器表达式包裹

Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. 

当函数被定义,装饰器表达式在含有函数定义的范围内被评估

The result must be a callable, which is invoked with the function object as the only argument. 

评估的结果必须是可以调用的,

The returned value is bound to the function name instead of the function object. 

Multiple decorators are applied in nested fashion. For example, the following code

@f1(arg)
@f2
def func(): pass

is roughly equivalent to

def func(): pass
func = f1(arg)(f2(func))

except that the original function is not temporarily bound to the name func.

When one or more 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 up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the 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.:

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 Calls. 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 ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.

Parameters may have annotations of the form “: expression” following the parameter name. Any parameter may have an annotation even those of the form *identifier or **identifier. Functions may have “return” annotation of the form “-> expression” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the __annotations__ attribute of the function object.

It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression 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 expression. The “def” form is actually more powerful since it allows the execution of multiple statements and annotations.

Programmer’s note: Functions are first-class objects. A “def” statement 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 Naming and binding for details.




有待更新........没事....看API练习下英语翻译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值