这章的主题是“抽象”。主要内容大概包括如何将语句组织成函数。有了函数以后就不必反反复复向计算机传递同样的指令了。还会介绍参数、作用域,递归的概念及其在程序中的用途。
使用下面的代码:
def hello(name): return 'Hello, ' + name + '!'>>> PRint hello('world')Hello, world!>>> print hello('Gumby')Hello, Gumby!如果在函数的开头写下字符串,他就会作为函数的一部分进行存储,这称为文档字符串。
使用下面的代码:
>>> def square(x):... 'Calculates the square of the number x.'... return x*x...>>> square.__doc__'Calculates the square of the number x.'内建的help函数是非常有用的。使用下面的代码:
>>> help(square)Help on function square in module __main__:square(x) Calculates the square of the number x.所有的函数都返回了东西,当不需要它们返回值的时候,它们就返回None
有时候,参数的顺序是很难记住的,为了让事情简单些,可以提供参数的名字。
使用下面的代码:
>>> def hello_1(greeting,name):... print ('%s, %s!'%(greeting,name))...>>> hello_1(greeting="Hello",name="world")Hello, world!这类使用参数名提供的参数叫做关键字参数。每个参赛的含义变得更加清晰,而且就算弄乱了参数的顺序,对于程序的功能也没有任何影响。
而且关键字参数在函数中给参数提供默认值。
有些时候让用户提供任意数量的参数是很有用的。这其实不难。
使用下面的代码:
>>> def print_params(*params):... print params...>>> print_params('Testing')('Testing',)>>>>>> print_params('Testing','param2')('Testing', 'param2')>>>>>> print_params(1,2,3)(1, 2, 3)将关键词参数和收集参数结合起来,这一部分觉得没什么用,先跳过了。
这一部分也跳过了。
只说一点,如果在一个函数内部要使用全局变量,需要用到global进行声明。使用下面的代码:
>>> x = 1>>> def change_global():... global x... x = x+1...>>> change_global()>>> x2这部分也跳过了
新闻热点
疑难解答