标签:class 组合 工作量 特殊 sum div eva for expected
def FunctionName(参数): ... 函数体 ... return #
下面给出一个函数示例,用于计算 n!:
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 expf.py temp = 6 [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat expf.py def countN(n): result = 1 for i in range(1, n+1): result *= i return result temp = countN(3) print(‘temp = ‘, temp)
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 myPrint.py Happy! Hello, World! [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat myPrint.py def myPrint(str = ‘Hello, World!‘): print(str) return myPrint(‘Happy!‘) myPrint()
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 somevalue.py Hurry in LemonTree ,he is 18 years old and graduated from GreedApple [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat somevalue.py def myPrint(name, school = ‘GreedApple‘, age = 18, hometown = ‘Pecato‘): print(name, ‘in‘, hometown, ‘,he is‘, age, ‘years old and graduated from‘, school) return myPrint(‘Hurry‘, hometown = ‘LemonTree‘)
4.定义默认参数时,默认参数必须指向不变对象,否则在多次调用函数时可能产生
错误的结果。因为,Python函数在定义的时候,默认参数的值就被计算出来了,在
每次调用该函数时,如果改变了默认参数的内容,则下次调用时,默认参数的内容
就变了,不再是函数定义时的值了。
可变参数:调用函数时,可变参数的传入值数量是可变的,类似于传入一个list或
tuple。只是,定义可变参数和定义一个list或tuple参数相比,仅仅在参数名前面
加了一个*号。并且Python允许你在list或tuple前面加一个*号,把list或tuple的
元素变成可变参数用来作为调用函数时的传入值。
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 sum.py 10 10 10 10 [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat sum.py def sum_one(value): sum = 0 for i in value: sum += i return sum def sum_two(*value): sum = 0 for i in value: sum += i return sum value = [1, 2, 3, 4] print(sum_one([1, 2, 3, 4])) print(sum_one(value)) print(sum_two(1, 2, 3, 4)) print(sum_two(*value))
关键字参数:与可变参数相比,关键字参数允许你传入0个或任意个含参数名的参
数,这些关键字参数在函数内部自动组装为一个dict。 只是,定义关键字参数和
定义一个dict参数相比,仅仅在参数名前面加了两个*号。在调用函数时,Python
允许dict前加两个*号,将其转换为关键字参数传进去
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 myPrint2.py Name: Hurry age: 18 {‘Number‘: 12345, ‘SchoolID‘: 54321} Name: Hurry age: 18 {‘Number‘: 12345, ‘SchoolID‘: 54321} [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat myPrint2.py def myPrint(name, age, **other): print(‘Name: ‘, name, ‘age:‘, age, other) return temp = {‘Number‘: 12345, ‘SchoolID‘: 54321} myPrint(‘Hurry‘, 18, **temp) myPrint(‘Hurry‘, 18, Number = 12345, SchoolID = 54321)
命名关键字参数:可以限制传入值KEY的关键字参数,定义时和关键字参数**kw不
同,命名关键字参数需要一个特殊分隔符*,*后面的参数被视为命名关键字参数。
[wangml@iZwz976helaylvgqok97prZ pythoncode]$ python3 myPrint3.py Name: Hurry age: 18 Number 12345 SchoolID 54321 Traceback (most recent call last): File "myPrint3.py", line 8, in <module> myPrint(‘Hurry‘, 18, Number = 12345, CardID = 54321) TypeError: myPrint() got an unexpected keyword argument ‘CardID‘ [wangml@iZwz976helaylvgqok97prZ pythoncode]$ cat myPrint3.py def myPrint(name, age, *, Number, SchoolID): print(‘Name: ‘, name, ‘age:‘, age, ‘Number‘, Number, ‘SchoolID‘, SchoolID) return temp = {‘Number‘: 12345, ‘SchoolID‘: 54321} myPrint(‘Hurry‘, 18, **temp) myPrint(‘Hurry‘, 18, Number = 12345, CardID = 54321)
标签:class 组合 工作量 特殊 sum div eva for expected
原文地址:https://www.cnblogs.com/lnlin/p/9053663.html