We can create a function that writes the Fibonacci series to an arbitrary boundary:
我们可以创建一个这样的函数,它在任意边界内书斐波拉切数列:
>>>
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
The keyword def introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented.
关键字def引出了函数定义。它后面必须跟函数名和形式参数的括号列表。构成函数主体的语句从下一行开始,必须缩进。
The first statement of the function body can optionally be a string literal; this string literal is the function’s documentation string, or docstring. (More about docstrings can be found in the section Documentation Strings.) There are tools which use docstrings to automatically produce online or printed documentation, or to let the user interactively browse through code; it’s good practice to include docstrings in code that you write, so make a habit of it.
函数体的第一句语句可以选择性使用字符文字;这个字符文件时函数的说明文档字符,或者文档。(更多的了解docstrings可以在文件字符串章节找到)使用docstrings的工具动态的在线生成或者打印文档,或者通过浏览代码的方式交互式的让用户浏览;在你的代码中写docstrings(译者说,即注释)是一个好的实践,所以请将它培养成你的习惯。
The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables and variables of enclosing functions cannot be directly assigned a value within a function (unless, for global variables, named in a globalstatement, or, for variables of enclosing functions, named in a nonlocal statement), although they may be referenced.
函数的执行引入了一个新的符号表,用于函数的局部变量。更确切来说,函数中的所有变量赋值都存储在局部符号表中;变量引用首先在本地符号表中查找,然后在封闭函数的本地符号表中查找,然后在全局符号表中查找,最后在内置名称表中查找。因此,全局变量和封闭函数变量不能够直接的使用函数赋值(除非,对于使用global语句的全局变量,或者,使用nonlocal语句的封闭函数),尽管他们可能被引用。
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). 1 When a function calls another function, a new local symbol table is created for that call.
函数调用的实际参数(参数)在被调用函数的本地符号表中引入。函数调用的实际参数(实参)在被调用函数的局部符号表中引入;因此,参数使用值调用传递(其中值始终是对象引用,而不是对象的值)。当一个函数调用另一个函数时,将为该调用创建一个新的本地符号表。
A function definition introduces the function name in the current symbol table. The value of the function name has a type that is recognized by the interpreter as a user-defined function. This value can be assigned to another name which can then also be used as a function. This serves as a general renaming mechanism:
在当前的符号表中,函数定义介绍了函数名称。函数名的值有一个类型,它被解释器识别为用户定义的函数。这个值可以赋给另一个名称,然后这个名称也可以作为函数使用。这是一个通用的重命名机制:
>>>
>>> fib<function fib at 10042ed0>
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89
Coming from other languages, you might object that fib is not a function but a procedure since it doesn’t return a value. In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name). Writing the value None is normally suppressed by the interpreter if it would be the only value written. You can see it if you really want to using print():
使用其他语言的话,你可能认为fib对象不是一个函数而是程序,因为它不返回值。事实上,即使函数没有一个return语句也是有返回值的,虽然被忽略(译者说,其实就是说return语句没有显示的写出来,但是实际是有的)。这个返回值叫做None(是一个内置名)。如果None是惟一写入的值,则解释器通常会禁止它。如果你真的想使用,你可以使用print()看到:
>>>
>>> fib(0)
>>> print(fib(0))
None
It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it:
写一个返回斐波拉切数列的函数是简单地,可以替代打印:
>>>
>>> def fib2(n): # return Fibonacci series up to n
... """Return a list containing the Fibonacci series up to n."""
... result = []
... a, b = 0, 1
... while a < n:
... result.append(a) # see below
... a, b = b, a+b
... return result
...
>>> f100 = fib2(100) # call it
>>> f100 # write the result
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
This example, as usual, demonstrates some new Python features:
通常,这个例子展示了一些python新特性:
[if !supportLists]· [endif]
The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.
Return语句从一个函数返回一个值。Return语句没有表达式参数的时候,其返回None。函数结束时也返回None。
[if !supportLists]· [endif]
The statement result.append(a) calls a method of the list object result. A method is a function that ‘belongs’ to an object and is named obj.methodname, where obj is some object (this may be an expression), and methodname is the name of a method that is defined by the object’s type. Different types define different methods. Methods of different types may have the same name without causing ambiguity. (It is possible to define your own object types and methods, using classes, see Classes) The method append() shown in the example is defined for list objects; it adds a new element at the end of the list. In this example it is equivalent to result = result + [a], but more efficient.
result.append(a) 语句调用了result列表对象的一个方法。一个方法是一个属于对象的函数,它被命名为obj.methodname,obj是某个对象(它可能是一个表达式),methodname是被对象类型定义的方法名称。不同的类型定位不同的方法。不同类型的方法肯呢过有相同的名称且不会有歧义。(使用类可以定义自己的对象类型和方法,具体见Classes章节)为列表对象定义了示例中所示的方法append();在列表最后添加新的元素。在这个例子中,它相当于result = result + [a],虽然有更多的不同。
网友评论