Skip to content

Commit 5f44a5a

Browse files
update README.md
1 parent ae798fe commit 5f44a5a

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
学习过程中遇到的问题以及解决方案,这些问题是我学习一路来遇到的问题以及一些解决方案[学习Python的过程中遇到的问题以及解决办法](https://github.com/dreamapplehappy/python3-tutorial/wiki/%E5%AD%A6%E4%B9%A0Python%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98)
44

5+
学习的进度如下所示:
6+
7+
+ [] x
8+
- [] c
9+
- [] d
10+
11+
12+
13+
14+
15+
16+
* `Tue Aug 15 2017 11:44:05 GMT+0800 (CST)` 关于Python的`functools.wraps`的问题[What does functools.wraps do?](https://stackoverflow.com/questions/308999/what-does-functools-wraps-do)
17+
* `Mon Aug 07 2017 23:16:42 GMT+0800 (CST)` 关于Python闭包以及`lambda`的问题[What do (lambda) function closures capture?](https://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture)
18+
* `Sun Aug 06 2017 15:21:38 GMT+0800 (CST)` 关于Python的三目运算符[Does Python have a ternary conditional operator?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator)
19+
* `Sun Aug 06 2017 12:42:34 GMT+0800 (CST)` 关于Python变量的作用域的问题[Python nonlocal statement](https://stackoverflow.com/questions/1261875/python-nonlocal-statement)
20+
* `Sun Aug 06 2017 01:21:10 GMT+0800 (CST)` 关于Python使用`list``index`方法查找元素,当查找的元素不存在的时候的处理方法[Best way to handle list.index(might-not-exist) in python?](https://stackoverflow.com/questions/2132718/best-way-to-handle-list-indexmight-not-exist-in-python)[Python index of item in list without error?](https://stackoverflow.com/questions/13160564/python-index-of-item-in-list-without-error)
21+
* `Sun Aug 06 2017 00:22:38 GMT+0800 (CST)` 关于Python的变量[Python variable declaration](https://stackoverflow.com/questions/11007627/python-variable-declaration)
22+
* `Thu Aug 03 2017 22:19:22 GMT+0800 (CST)` 关于Python为什么没有`++``--`运算符[Why are there no ++ and --​ operators in Python?](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python)
23+
* `Mon Jul 31 2017 10:45:52 GMT+0800 (CST)` 关于内置函数[enumerate](https://docs.python.org/3/library/functions.html#enumerate)
24+
* `Sun Jul 30 2017 22:11:50 GMT+0800 (CST)` 关于切片的一篇文章,可以加深对切片的理解[How to Slice Lists/Arrays and Tuples in Python](http://pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/)
25+
* `Sat Jul 29 2017 23:36:07 GMT+0800 (CST)` 关于如何展开一个列表参数[Unpacking Argument Lists](https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists)
26+
* `Sat Jul 29 2017 23:11:04 GMT+0800 (CST)` 关于Python中`is``==`的区别[Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?](https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce)[String comparison in Python: is vs. == ](https://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs)
27+
* `Mon Jul 24 2017 23:57:15 GMT+0800 (CST)` [关于格式化输出多个值](http://www.python-course.eu/python3_formatted_output.php)
28+
* `Mon Jul 24 2017 23:16:48 GMT+0800 (CST)` [关于数字的有限和无限问题](https://stackoverflow.com/questions/5438745/python-nan-and-inf-values)
29+
30+
531
MIT License
632

733
Copyright (c) [2017] [@dreamapplehappy](https://github.com/dreamapplehappy)

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1. 写一篇文章,详细的介绍lambda

day-11/anonymous-func.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# use lambda create a anonymous function
3+
anonymous = lambda x: x * x
4+
print(anonymous)
5+
6+
l = list(map(lambda x: x + 2, [1, 2, 3, 4]))
7+
print(l)
8+
9+
# generator
10+
def generator_square_number(max):
11+
index = 1
12+
while(index < max):
13+
index += 1
14+
yield lambda index=index: index * index
15+
16+
gsn = generator_square_number(10)
17+
18+
print(next(gsn)())
19+
print(next(gsn)())
20+
print(next(gsn)())

day-11/decorator.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# decorator
2+
def compute(x):
3+
return x + 1
4+
5+
print(compute.__name__)
6+
7+
# decorator is a high-level function
8+
def log(func):
9+
def wrapper(*args, **kw):
10+
print('func call', func.__name__)
11+
return func(*args, **kw)
12+
return wrapper
13+
14+
# decorator
15+
@log
16+
def sum():
17+
pass
18+
19+
sum()
20+
21+
def logger(text):
22+
def decorator(func):
23+
def wrapper(*args, **kw):
24+
print('your args', text, func.__name__)
25+
return func(*args, **kw)
26+
return wrapper
27+
return decorator
28+
29+
@logger('test')
30+
def sum1():
31+
pass
32+
33+
sum1()
34+
35+
# original function __name__ change
36+
print(sum1.__name__)
37+
38+
# fix the problem
39+
40+
import functools
41+
42+
def optimization_logger(*text):
43+
def decorator(func):
44+
@functools.wraps(func)
45+
def wrapper(*args, **kw):
46+
print('your args', text, func.__name__)
47+
return func(*args, **kw)
48+
return wrapper
49+
return decorator
50+
51+
@optimization_logger()
52+
def sum2():
53+
pass
54+
55+
sum2()
56+
print(sum2.__name__)
57+
58+

day-11/return-func.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 返回函数
2+
3+
def calc_sum(*arg):
4+
sum = 0
5+
for num in arg:
6+
sum += num
7+
return sum
8+
9+
print(calc_sum(1, 2, 3)) # 6
10+
11+
def lazy_sum(*arg):
12+
def sum():
13+
lsum = 0
14+
for num in arg:
15+
lsum += num
16+
return lsum
17+
return sum
18+
19+
print(lazy_sum(1,2,3))
20+
print(lazy_sum(1,2,3)())
21+
22+
def count():
23+
l = []
24+
for n in range(1,4):
25+
def f():
26+
return n * n
27+
l.append(f)
28+
return l
29+
30+
f1, f2, f3 = count()
31+
print(f1, f2, f3)
32+
print(f1(), f2(), f3())
33+
34+
print(range(4)) # range(0, 4)
35+
print(range(1,4)) # range(1, 4)
36+
37+
38+
# 使用闭包
39+
40+
def closure_count():
41+
l = []
42+
for n in range(4):
43+
def compute(k):
44+
def c():
45+
return k * k
46+
return c
47+
l.append(compute(n))
48+
return l
49+
50+
c0, c1, c2, c3 = closure_count()
51+
52+
print(c0, c1, c2, c3)
53+
print(c0(), c1(), c2(), c3())
54+
55+
print('--- divider ---')
56+
def closure_count2():
57+
l = []
58+
for n in range(4):
59+
# 添加默认参数
60+
l.append(lambda x = n: x * x)
61+
return l
62+
63+
a0, a1, a2, a3 = closure_count2()
64+
print(closure_count2())
65+
print(a0, a1, a2, a3)
66+
print(a0(), a1(), a2(), a3())
67+

0 commit comments

Comments
 (0)