Files
notes_estom/Python/python3/4函数.md
2020-09-23 13:41:55 +08:00

283 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 定义函数
```py
>>> 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
```
> 参数都是引用传递。
* 关键字 def 引入一个函数 定义。它必须后跟函数名称和带括号的形式参数列表。构成函数体的语句从下一行开始,并且必须缩进。
* 函数体的第一个语句可以(可选的)是字符串文字。用来生成文档
* 函数的 执行 会引入一个用于函数局部变量的新符号表。 更确切地说,函数中所有的变量赋值都将存储在局部符号表中;而变量引用会首先在局部符号表中查找,然后是外层函数的局部符号表,再然后是全局符号表,最后是内置名称的符号表。
* 在函数被调用时,实际参数(实参)会被引入被调用函数的本地符号表中;因此,实参是通过 按值调用 传递的(其中 值 始终是对象 引用 而不是对象的值。1 当一个函数调用另外一个函数时,将会为该调用创建一个新的本地符号表。
## 默认参数(可选参数)(函数定义)
```py
def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
```
这个函数可以通过几种方式调用:
* 只给出必需的参数ask_ok('Do you really want to quit?')
* 给出一个可选的参数ask_ok('OK to overwrite the file?', 2)
* 或者给出所有的参数ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
> 多次调用默认参数会共享
```
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
```
## 位置参数和关键字参数(函数调用)
也可以使用形如 kwarg=value 的 关键字参数 来调用函数。例如下面的函数:
```py
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.")
print("-- Lovely plumage, the", type)
print("-- It's", state, "!")
```
接受一个必需的参数voltage和三个可选的参数state, action和 type。这个函数可以通过下面的任何一种方式调用:
```py
parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
```
但下面的函数调用都是无效的:
```py
parrot() # required argument missing
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
parrot(110, voltage=220) # duplicate value for the same argument
parrot(actor='John Cleese') # unknown keyword argument
```
* 在函数调用中,关键字参数必须跟随在位置参数的后面。
* 传递的所有关键字参数必须与函数接受的其中一个参数匹配(比如 actor 不是函数 parrot 的有效参数),它们的顺序并不重要。
* 不能对同一个参数多次赋值。
## 复合参数(函数定义)
当存在一个形式为 `**name` 的最后一个形参时,它会接收一个字典 ,其中包含除了与已有形参相对应的关键字参数以外的所有关键字参数。
这可以与一个形式为 `*name`,接收一个包含除了已有形参列表以外的位置参数的 元组 的形参组合使用 (*name 必须出现在 **name 之前。)
例如,如果我们这样定义一个函数:
```py
def cheeseshop(kind, *arguments, **keywords):
print("-- Do you have any", kind, "?")
print("-- I'm sorry, we're all out of", kind)
for arg in arguments:
print(arg)
print("-" * 40)
for kw in keywords:
print(kw, ":", keywords[kw])
```
它可以像这样调用:
```py
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
```
当然它会打印:
```
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch
```
注意打印时关键字参数的顺序保证与调用函数时提供它们的顺序是相匹配的。
## 特殊参数(函数定义)
默认情况下,函数的参数传递形式可以是位置参数或是显式的关键字参数。 为了确保可读性和运行效率,限制允许的参数传递形式是有意义的,这样开发者只需查看函数定义即可确定参数项是仅按位置、按位置也按关键字,还是仅按关键字传递。
函数的定义看起来可以像是这样:
```py
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
----------- ---------- ----------
| | |
| Positional or keyword |
| - Keyword only
-- Positional only
```
* / 和 * 是可选的。 如果使用这些符号则表明可以通过何种形参将参数值传递给函数:仅限位置、位置或关键字,以及仅限关键字。 关键字形参也被称为命名形参。
* 如果函数定义中未使用 / 和 *,则参数可以按位置或按关键字传递给函数。
### 函数实例
* 请考虑以下示例函数定义并特别注意 / 和 * 标记:
```py
>>>
>>> def standard_arg(arg):
... print(arg)
...
>>> def pos_only_arg(arg, /):
... print(arg)
...
>>> def kwd_only_arg(*, arg):
... print(arg)
...
>>> def combined_example(pos_only, /, standard, *, kwd_only):
... print(pos_only, standard, kwd_only)
```
* 第一个函数定义 standard_arg 是最常见的形式,对调用方式没有任何限制,参数可以按位置也可以按关键字传入:
```py
>>>
>>> standard_arg(2)
2
>>> standard_arg(arg=2)
2
```
* 第二个函数 pos_only_arg 在函数定义中带有 /,限制仅使用位置形参。:
```py
>>>
>>> pos_only_arg(1)
1
>>> pos_only_arg(arg=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pos_only_arg() got an unexpected keyword argument 'arg'
```
* 第三个函数 kwd_only_args 在函数定义中通过 * 指明仅允许关键字参数:
```
>>>
>>> kwd_only_arg(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: kwd_only_arg() takes 0 positional arguments but 1 was given
>>> kwd_only_arg(arg=3)
3
```
* 而最后一个则在同一函数定义中使用了全部三种调用方式:
```
>>>
>>> combined_example(1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: combined_example() takes 2 positional arguments but 3 were given
>>> combined_example(1, 2, kwd_only=3)
1 2 3
>>> combined_example(1, standard=2, kwd_only=3)
1 2 3
>>> combined_example(pos_only=1, standard=2, kwd_only=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: combined_example() got an unexpected keyword argument 'pos_only'
```
* 最后,请考虑这个函数定义,它的位置参数 name 和 **kwds 之间由于存在关键字名称 name 而可能产生潜在冲突:
```
def foo(name, **kwds):
return 'name' in kwds
```
任何调用都不可能让它返回 True因为关键字 'name' 将总是绑定到第一个形参。 例如:
```
>>>
>>> foo(1, **{'name': 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() got multiple values for argument 'name'
>>>
```
* 但使用 / (仅限位置参数) 就可能做到,因为它允许 name 作为位置参数,也允许 'name' 作为关键字参数的关键字名称:
```
def foo(name, /, **kwds):
return 'name' in kwds
>>> foo(1, **{'name': 2})
True
```
* 换句话说,仅限位置形参的名称可以在 **kwds 中使用而不产生歧义。
### 使用指导
* 如果你希望形参名称对用户来说不可用,则使用仅限位置形参。 这适用于形参名称没有实际意义,以及当你希望强制规定调用时的参数顺序,或是需要同时收受一些位置形参和任意关键字形参等情况。
* 当形参名称有实际意义,以及显式指定形参名称可使函数定义更易理解,或者当你想要防止用户过于依赖传入参数的位置时,则使用仅限关键字形参。
* 对于 API 来说,使用仅限位置形参可以防止形参名称在未来被修改时造成破坏性的 API 变动。
## 解包参数列表(函数调用)
当参数已经在列表或元组中但要为需要单独位置参数的函数调用解包时,会发生相反的情况。例如,内置的 range() 函数需要单独的 start 和 stop 参数。如果它们不能单独使用,可以使用 * 操作符 来编写函数调用以便从列表或元组中解包参数:
```py
>>>
>>> list(range(3, 6)) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
```
同样的方式,字典可使用 ** 操作符 来提供关键字参数:
```py
>>>
>>> def parrot(voltage, state='a stiff', action='voom'):
... print("-- This parrot wouldn't", action, end=' ')
... print("if you put", voltage, "volts through it.", end=' ')
... print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
```
> 使用*和**来对元组参数和字典参数进行解包,与定义复合参数的函数相对应。
## Lambda表达式函数定义
可以用 lambda 关键字来创建一个小的匿名函数。这个函数返回两个参数的和: lambda a, b: a+b 。Lambda函数可以在需要函数对象的任何地方使用。它们在语法上限于单个表达式。从语义上来说它们只是正常函数定义的语法糖。与嵌套函数定义一样lambda函数可以引用所包含域的变量:
```py
>>>
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
```