@@ -492,6 +492,15 @@ def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
492492 -- Positional only
493493```
494494
495+ ``` txt
496+ def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
497+ ----------- ---------- ----------
498+ | | |
499+ | 位置或关键字 |
500+ | - 仅限关键字
501+ -- 仅限位置
502+ ```
503+
495504` / ` 和 ` * ` 是可选的。这些符号表明形参如何把参数值传递给函数:位置、位置或关键字、关键字。关键字形参也叫作命名形参。
496505
497506#### 4.8.3.1 位置或关键字参数
@@ -544,3 +553,104 @@ print(concat("earth", "mars", "venus", sep="."))
544553```
545554
546555:::
556+
557+ ### 4.8.5 解包实参列表
558+
559+ 函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。
560+ 例如,内置的 range() 函数要求独立的 start 和 stop 实参。
561+ 如果这些参数不是独立的,则要在调用函数时,用 ` * ` 操作符把实参从列表或元组解包出来:
562+
563+ ``` python
564+ list (range (3 , 6 )) # 两个参数的正常调用
565+ # [3, 4, 5]
566+ args = [3 , 6 ]
567+ list (range (* args)) # 从一个数组解包出参数的调用
568+ # [3, 4, 5]
569+ ```
570+
571+ 同样,字典可以用 ` ** ` 操作符传递关键字参数:
572+
573+ ``` python
574+ def parrot (voltage , state = ' a stiff' , action = ' voom' ):
575+ print (" -- This parrot wouldn't" , action, end = ' ' )
576+ print (" if you put" , voltage, " volts through it." , end = ' ' )
577+ print (" E's" , state, " !" )
578+
579+ d = {" voltage" : " four million" , " state" : " bleedin' demised" , " action" : " VOOM" }
580+ parrot(** d)
581+ # This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
582+ ```
583+
584+ ## 4.9 Lambda 表达式
585+
586+ lambda 关键字用于 _ 创建小巧的匿名函数_ 。lambda a, b: a+b 函数返回两个参数的和。Lambda 函数可用于任何需要 _ 函数对象_ 的地方。
587+ 在语法上,匿名函数只能是单个表达式。在语义上,它只是常规函数定义的语法糖。与嵌套函数定义一样,lambda 函数可以引用包含作用域中的变量:
588+
589+ ``` python
590+ def make_incrementor (n ):
591+ return lambda x : x + n
592+
593+ f = make_incrementor(42 )
594+ f(0 )
595+ # 42
596+ f(1 )
597+ # 43
598+ ```
599+
600+ 上例用 lambda 表达式返回函数。还可以把匿名函数用作传递的实参:
601+
602+ ``` python
603+ pairs = [(1 , ' one' ), (2 , ' two' ), (3 , ' three' ), (4 , ' four' )]
604+ pairs.sort(key = lambda pair : pair[1 ])
605+ pairs
606+ # [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
607+ ```
608+
609+ ## 4.10 文档字符串
610+
611+ 以下是文档字符串内容和格式的约定。
612+
613+ - 第一行应为对象用途的简短摘要。
614+ 这一行应以大写字母开头,以句点结尾。
615+
616+ - 文档字符串为多行时,第二行应为空白行,在视觉上将摘要与其余描述分开。
617+ 后面的行可包含若干段落,描述对象的调用约定、副作用等。
618+
619+ - 文档字符串第一行 之后 的第一个非空行决定了整个文档字符串的缩进量。
620+ 第一行通常与字符串开头的引号相邻,其缩进在字符串中并不明显,因此,不能用第一行的缩进。
621+ 然后,删除字符串中所有行开头处与此缩进“等价”的空白符。
622+ 不能有比此缩进更少的行,但如果出现了缩进更少的行,应删除这些行的所有前导空白符。
623+
624+ ``` python
625+ def my_function ():
626+ """ Do nothing, but document it.
627+
628+ No, really, it doesn't do anything.
629+ """
630+ pass
631+
632+ print (my_function.__doc__ )
633+ # Do nothing, but document it.
634+ #
635+ # No, really, it doesn't do anything.
636+ ```
637+
638+ ## 4.11 函数注解
639+
640+ 函数注解 是可选的用户自定义函数类型的元数据完整信息。
641+
642+ 标注 以字典的形式存放在函数的 ` __annotations__ ` 属性中而对函数的其他部分没有影响。
643+
644+ 下面的示例有一个必须的参数、一个可选的关键字参数以及返回值都带有相应的标注:
645+
646+ ``` python
647+ def f (ham : str , eggs : str = ' eggs' ) -> str :
648+ print (" Annotations:" , f.__annotations__ )
649+ print (" Arguments:" , ham, eggs)
650+ return ham + ' and ' + eggs
651+
652+ f(' spam' )
653+ # Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}
654+ # Arguments: spam eggs
655+ # 'spam and eggs'
656+ ```
0 commit comments