-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.py
94 lines (73 loc) · 2.17 KB
/
parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
File: parameter.py
Project: 07-function
File Created: Saturday, 25th July 2020 4:20:32 pm
Author: lanling (https://github.com/muyuuuu)
-----------
Last Modified: Saturday, 25th July 2020 4:20:35 pm
Modified By: lanling (https://github.com/muyuuuu)
Copyright 2020 - 2020 NCST, NCST
-----------
@ 佛祖保佑,永无BUG--
'''
from functools import partial
import math
# 接受任意数量的位置参数
def avg(first, *rest):
return first + sum(rest)
print(avg(1, 2))
print(avg(1, 2, 3, 4))
# 接受任意数量的关键字参数
# attrs是一个包含所有被传入进来的关键字参数的字典。
def foo(*args, **kwargs):
print('args = ', args)
print('kwargs = ', kwargs)
if 'a' in kwargs.keys():
print(kwargs['a'])
# print(kwargs['a'])
print('---------------------------------------')
foo(1, 2, 3, 4)
foo(a = 1, b = 2, c = 3)
foo(1, 2, 3, 4, a = 1, b = 2, c = 3)
foo('a', 1, None, a = 1, b = '2', c = 3)
# 同时接受任意数量的位置参数和关键字参数
# *args 只能作为最后一个位置参数
def anyargs(*args, **kwargs):
# 位置参数放到元祖中
print(args) # A tuple
# 关键字参数放到字典中
print(kwargs) # A dict
# 希望函数的某些参数强制使用关键字参数传递
def recv(maxsize, *, block):
return 2
# recv(1024, True) # TypeError
# 必须传递关键字的参数
recv(1024, block=True) # Ok
# 预先指定关键字参数
def minimum(*values, clip=None):
m = min(values)
if clip is not None:
m = clip if clip > m else m
return m
print(minimum(1, 5, 2, -5, 10))
print(minimum(1, 5, 2, -5, 10, clip=0))
# 减少可调用对象的参数个数
def spam(a, b, c, d):
print(a, b, c, d)
# 按照参数顺序传递参数
s1 = partial(spam, 1)
print(s1(2, 3, 4))
s3 = partial(spam, 1, 2, d=42)
print(s3(6))
# 多个数值的比较并排序
center = (4.2, 6.7)
points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
def distance(p1, p2):
x1, y1 = p1
x2, y2 = p2
return math.hypot(x2 - x1, y2 - y1)
# 只传入第一个参数,points为第二个传入参数
points.sort(key=partial(distance, center))
print(points)
# 等价
points.sort(key=lambda p: distance(center, p))