Skip to content

Commit f6ffbef

Browse files
author
bianxiaokai
committed
feat: Lambda.py
1 parent fcb6317 commit f6ffbef

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

function.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def move(x, y, step, angle=0):
4545
def quadratic(a, b, c):
4646
if not isinstance(a * b * c, (float, int)):
4747
raise TypeError("quadratic must be a float or int")
48-
q = b**2 - 4 * a * c
48+
q = b ** 2 - 4 * a * c
4949
if q < 0:
5050
print('no solution')
5151
elif q == 0:
@@ -76,13 +76,14 @@ def add_end(L=None):
7676
def calc(*numbers):
7777
sum = 0
7878
for n in numbers:
79-
sum += n**2
79+
sum += n ** 2
8080
return sum
8181

8282

8383
nums = [1, 2, 3]
8484
calc(*nums)
8585

86+
8687
# 关键字参数
8788
# 允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict
8889

@@ -97,6 +98,7 @@ def person(name, age, **kw):
9798
extra = {'city': 'Beijing', 'job': 'Engineer'}
9899
person('Bob', 35, **extra)
99100

101+
100102
# 命名关键字参数
101103
# 函数的调用者可以传入任意不受限制的关键字参数, *后面的参数被视为命名关键字参数
102104

@@ -191,3 +193,39 @@ def move(n, a, b, c):
191193

192194

193195
move(3, 'A', 'B', 'C')
196+
197+
198+
# 返回函数
199+
def lazy_sum(*args):
200+
def sum():
201+
ax = 0
202+
for n in args:
203+
ax = ax + n
204+
return ax
205+
206+
return sum
207+
208+
209+
f = lazy_sum(1, 2, 3, 4, 5)
210+
print(f())
211+
212+
213+
# 闭包
214+
def count():
215+
fs = []
216+
217+
def f(j):
218+
def g():
219+
return j * j
220+
221+
return g
222+
223+
for i in range(1, 4):
224+
fs.append(f(i))
225+
return fs
226+
227+
228+
f1, f2, f3 = count()
229+
print(f1())
230+
print(f2())
231+
print(f3())

0 commit comments

Comments
 (0)