Skip to content

Commit b2232ab

Browse files
author
bianxiaokai
committed
module
1 parent 85ffaaa commit b2232ab

File tree

9 files changed

+91
-4
lines changed

9 files changed

+91
-4
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/python-demo.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

function.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,26 @@ def g():
229229
print(f1())
230230
print(f2())
231231
print(f3())
232+
233+
# 一个函数可以返回一个计算结果,也可以返回一个函数。
234+
235+
# 返回一个函数时,牢记该函数并未执行,返回函数中不要引用任何可能会变化的变量。
236+
237+
def createCounter():
238+
x = 0
239+
def counter():
240+
nonlocal x # 如果对外层变量赋值,由于Python解释器会把x当作函数fn()的局部变量,它会报错:
241+
x = x + 1
242+
return x
243+
return counter
244+
245+
246+
counterA = createCounter()
247+
print(counterA(), counterA(), counterA(), counterA(), counterA())
248+
249+
# lambda函名函数
250+
def is_odd(n):
251+
return n % 2 == 1
252+
253+
L = list(filter(lambda n: n % 2 == 1, range(1, 20)))
254+
print(L)

if.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
# else:
66
# print('00后')
77

8-
print(84.5 / (1.75 ** 2))
9-
10-
height = 1.75
11-
weight = 83.3
8+
height = 1.76
9+
weight = 75
1210

1311
bmi = weight / (height ** 2)
1412
print('bmi = %.f' % bmi)

partial.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from functools import partial
2+
3+
int2 = partial(int, base=2)
4+
5+
print(int2('1010101'))
6+
7+
8+
# int2 = functools.partial(参数1,参数2,参数3)
9+
# 参数1:函数对象,
10+
# 参数2:*args 可变参数,接收tuple,list
11+
# 参数3:*kw 关键字参数,接收dict
12+
13+
# int2 = functools.partial(int,10,base=10)
14+
15+
# 参数1必填,参数2和参数3可省略,那就和原函数没区别了,因为参数1就是原函数,参数2就是可变参数,参数3为关键字参数,int自带关键字参数base,当不传为默认值,传入时必须以base=xxx的形式,对应原始的关键字,参数2传入的话会组装成tuple或list,
16+
# 再通过*args 传入int(*args)
17+
18+
# 偏函数可以简化参数操作。
19+
# 当函数的某个参数是我们可以提前获知的,那我们就可以将它固定住!
20+
# 定义一个取余函数,默认和2取余;
21+
def mod(x, y=2):
22+
# 返回 True 或 False
23+
return x % y == 0
24+
25+
26+
# 假设我们要计算和3取余,如果不使用partial()函数,那么我们每次调用mod()函数时,都要写y=3
27+
mod(4, y=3)
28+
mod(6, y=3)
29+
30+
# 使用partial()函数
31+
mod_3 = partial(mod, y=3)

0 commit comments

Comments
 (0)