Skip to content

Commit d35e279

Browse files
committed
添加了代码文件
1 parent 1107480 commit d35e279

File tree

47 files changed

+1376
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1376
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

009 装饰器/innerpar.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# def outer(x):
2+
# def plus(a, b):
3+
# return a + b
4+
#
5+
# for i in range(10):
6+
# x = plus(x, 10)
7+
# return x
8+
9+
def logger(oldFunc):
10+
"""夹层装饰器,给函数调用前后加两个打印突出显示"""
11+
def newFunc():
12+
print("~~~~~~~~~~~")
13+
oldFunc()
14+
print("~~~~~~~~~~~")
15+
...
16+
17+
return newFunc
18+
19+
20+
def logger2(oldFunc):
21+
"""夹层装饰器,只给头顶增加一个打印"""
22+
def newFunc():
23+
print("+++++++++++")
24+
oldFunc()
25+
...
26+
27+
return newFunc
28+
29+
30+
def runDouble(oldFunc):
31+
"""给一个函数装饰,装饰后这个函数就能运行两次了"""
32+
def newFunc():
33+
oldFunc()
34+
oldFunc()
35+
36+
return newFunc
37+
38+
39+
def counter(oldFunc):
40+
"""记录一个函数打印了多少次"""
41+
count = 0
42+
43+
def newFunc():
44+
nonlocal count
45+
count += 1
46+
oldFunc()
47+
print(f"这个函数{oldFunc.__name__}运行了{count}次")
48+
...
49+
50+
return newFunc
51+
52+
53+
def shield(oldFunc):
54+
"""防护盾装饰器,让一个函数在运行中不会发生错误导致程序终止"""
55+
def newFunc():
56+
try:
57+
oldFunc()
58+
except Exception as e:
59+
print(f"函数{oldFunc.__name__}出错了", oldFunc, e)
60+
61+
return newFunc()
62+
63+
64+
from functools import lru_cache
65+
66+
67+
@lru_cache(None)
68+
def func():
69+
print("hello")
70+
71+
72+
@lru_cache()
73+
def func2():
74+
...
75+
76+
77+
class Check(object):
78+
"""类装饰器"""
79+
def __init__(self, fn):
80+
"""
81+
传入的是被装饰的原的函数
82+
"""
83+
self.__fn = fn
84+
85+
def __call__(self, *args, **kwargs): # 实现__call__方法,表示对象是一个可调用对象,可以像调用函数一样进行调用
86+
print("登录") # 在这之前可以写登录代码
87+
self.__fn()
88+
89+
90+
@Check # @Check <===> comment = Check(comment)
91+
def comment():
92+
print("正在发表评论")
93+
94+
95+
def robot(oldFunc):
96+
def newFunc(*args):
97+
print("started")
98+
res = oldFunc(*args)
99+
return res
100+
101+
return newFunc
102+
103+
104+
@robot
105+
def f():
106+
...
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
3+
4+
def timer(time_type):
5+
print(time_type)
6+
7+
def outer(func):
8+
print("func的名字是:", func.__name__)
9+
10+
def inner(*args, **kwargs):
11+
start = time.time()
12+
func(*args, **kwargs)
13+
end = time.time()
14+
res = end - start
15+
print('时间结果是:', res)
16+
17+
return inner
18+
19+
return outer
20+
21+
22+
@timer('min') # foo = timer(foo)
23+
# @outer
24+
def foo(a, b, c):
25+
print('in foo', a, b, c)
26+
27+
28+
foo('woniu', 'gfifigf', '79846543635')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import time
2+
3+
"""
4+
带参数的选择器
5+
timer() 加小括号调用结束之后,本身会变成一个不带参数的装饰器(小机器人)
6+
然后这个装饰器再对函数进行装饰
7+
"""
8+
def timer(time_type):
9+
10+
print(time_type)
11+
if time_type == "min":
12+
def robot(func):
13+
print("func的名字是:", func.__name__)
14+
15+
def inner(*args, **kwargs):
16+
start = time.time()
17+
func(*args, **kwargs)
18+
end = time.time()
19+
res = end - start
20+
print('时间结果是:', res)
21+
22+
return inner
23+
else:
24+
def robot(func):
25+
print("func的名字是:", func.__name__)
26+
27+
def inner(*args, **kwargs):
28+
start = time.time()
29+
func(*args, **kwargs)
30+
end = time.time()
31+
res = end - start
32+
print('时间结果是:', res)
33+
34+
return inner
35+
return robot
36+
37+
38+
@timer('min')
39+
def foo(a, b, c):
40+
print('in foo', a, b, c)
41+
42+
43+
foo('a', 'bb', 'ccc')

012 三目运算/main.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""
2+
三目运算符是什么意思?
3+
这个目又叫元,就是变量的个数
4+
5+
一目运算符,又叫一元运算,只参与一个数字,或者表达式
6+
not(...) 3! = 6
7+
8+
二元运算符
9+
__ 运算符 __
10+
a + b
11+
12+
三目运算符,
13+
__ 运算符 __ 运算符 __
14+
三目运算符有两个符号组成,这两个符号把整个一行分成了三个部分,这三个部分里写你想要写的东西
15+
16+
如果你是一个数学家,你可以发明四目运算符了
17+
__ 运算符 __ 运算符 __ 运算符 __
18+
19+
"""
20+
a = 15
21+
22+
# if a == 15:
23+
# print("yes")
24+
# else:
25+
# print("no")
26+
27+
28+
# 以下两个写法哪个才是正确的?
29+
a == 15 if print("yes1") else print("no1")
30+
31+
print("yes2") if a == 15 else print("no2")
32+
33+
# 原理解释
34+
# if None:
35+
# print("111")
36+
# else:
37+
# print("222")
38+
#
39+
# print(bool(None))
40+
41+
# if print("in if"):
42+
# print("111")
43+
# else:
44+
# print("222")
45+
46+
# 可以简化成一行,但是没必要,看着不舒服
47+
# if a < 100: print(122)
48+
# for 也可以简化成一行
49+
# for i in range(10): print(i)
50+
51+
# 注意: 和js写法是不一样的
52+
"""
53+
var age = 15;
54+
console.log(age<18 ? '未成年' : '成年');
55+
js 里的判断条件是在左边,中间是成立条件,右边是不成立条件
56+
区别对比:
57+
python中间是条件,左边挨着if的时成立,else的时不成立,这个从单词角度就很好记忆
58+
js、C语言,java、C++、php里的写法都一样符号也都一样,唯独python不一样。
59+
约?可:爬
60+
61+
如果你最开始学的不是python,你可能就会好奇感觉自己三目运算符总是写不对
62+
于是就不想用python写三目运算符了。
63+
64+
顺便补充一句。Go语言没有三目运算符,他们觉得可读性很低,没必要炫技术。
65+
如果python是你第一个学习的语言,那么你一定要区别清楚这个三目运算符
66+
67+
学习三目运算符的意义:
68+
1 别人这样写你能看懂
69+
2 看懂一些源码,第三方库
70+
3 可以多一种写代码的方式,抄作业不容易被看成雷同代码
71+
4 想发挥一行代码炫技的极简精神,就要熟练掌握这个
72+
"""
73+
74+
# 带返回值
75+
# b = print(111) if True else print(222)
76+
# print(b)
77+
# b = print(111) if False else print(222)
78+
# print(b)
79+
#
80+
# age = 15
81+
# res = "可以进入" if age >= 18 else "不可以进入"
82+
# print(res)
83+
84+
# 嵌套写法
85+
score = 83
86+
if score in range(101):
87+
if score < 60:
88+
print("不合格")
89+
else:
90+
print("合格")
91+
else:
92+
print("成绩错误")
93+
94+
# ... if score in range(101) else ...
95+
96+
# ... if score in range(101) else print("成绩错误")
97+
98+
print("不合格") if score < 60 else print("合格") if score in range(101) else print("成绩错误")
99+
100+
# 如果你看到你的队友和你合作项目写了这样的代码,立刻把它的代码改成普通写法,并告诉他不要炫技
101+
102+
# if elif else 多分支可以简化成一行吗?
103+
name = "xxx"
104+
105+
if name == "":
106+
print("空")
107+
elif name == "张三":
108+
print("找到人了")
109+
else:
110+
print("没找到")
111+
112+
# print("空") if name == "" elif print("找到人了") else print("没找到")
113+
# 这成了四目运算符了,没有这样的写法
114+
115+
# 小应用,让一个函数只有一半的几率区执行它
116+
117+
from random import random
118+
119+
120+
def do():
121+
...
122+
123+
124+
if random() < 0.5:
125+
do()
126+
127+
# if random(): do() # 这样写pycharm会有小警告,让强迫症的你不舒服
128+
# do () if random() < 0.5 else pass # 不能写pass,要换成点点点,
129+
# 以前的视频里有讲过pass和点点点的区别,不知道的可以去看一下我以前的视频
130+
do() if random() < 0.5 else ...

012 三目运算/test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
a = 15
2+
3+
a == 15 if print("yes1") else print("no1")
4+
print("yes2") if a == 15 else print("no2")
5+
6+
if bool(None):
7+
print(111)
8+
else:
9+
print(222)
10+
11+
if print("if if"):
12+
print(111)
13+
14+
15+
16+
17+
18+
# 嵌套写法
19+
score = 83
20+
if score in range(101):
21+
if score < 60:
22+
print("不合格")
23+
else:
24+
print("合格")
25+
else:
26+
print("成绩错误")
27+
28+
print("不合格") if score < 60 else print("合格") if score in range(101) else print("成绩错误")
29+
30+
from random import random
31+
32+
def do():
33+
...
34+
35+
if random() < 0.5:
36+
do()
37+
38+
do() if random() < 0.5 else ...
39+
40+
41+
42+
value = 65
43+
44+
res = "ok" if value > 60 else "no"
45+
# print("空") if name == "" elif print("找到人了") else print("没找到")

012 元组小逗号/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# a = [1]
2+
# a.append(1)
3+
b = (1,)
4+
for i in range(10000):
5+
b += (1,)
6+
print(b)

0 commit comments

Comments
 (0)