5年 Python 功力,总结了 10 个开发技巧
import inspect
def add(x, y):
return x + y
print
print(inspect.getsource(add))
===================
def add(x, y):
return x + y
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened")
File "demo.py", line 2, in <module>
print(1 / 0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
call last):
File "demo.py", line 4, in <module>
raise RuntimeError("Something bad happened")
RuntimeError: Something bad happened
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened") from exc
File "demo.py", line 2, in <module>
print(1 / 0)
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
call last):
File "demo.py", line 4, in <module>
raise RuntimeError("Something bad happened") from exc
RuntimeError: Something bad happened
print(1 / 0)
except Exception as exc:
raise RuntimeError("bad thing").with_traceback(exc)
try:
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened") from None
$
$ python demo.py
Traceback (most recent call last):
File "demo.py", line 4, in <module>
raise RuntimeError from None
RuntimeError: Something bad happened
(PythonCodingTime)
>>> from pprint import pprint
>>> pprint(sys.path)
['',
'/usr/local/Python3.7/lib/python37.zip',
'/usr/local/Python3.7/lib/python3.7',
'/usr/local/Python3.7/lib/python3.7/lib-dynload',
'/home/wangbm/.local/lib/python3.7/site-packages',
'/usr/local/Python3.7/lib/python3.7/site-packages']
>>>
/usr/lib/python2.7/site-packages/pip-18.1-py2.7.egg
/usr/lib/python2.7/site-packages/redis-3.0.1-py2.7.egg
/usr/lib64/python27.zip
/usr/lib64/python2.7
/usr/lib64/python2.7/plat-linux2
/usr/lib64/python2.7/lib-tk
/usr/lib64/python2.7/lib-old
/usr/lib64/python2.7/lib-dynload
/home/wangbm/.local/lib/python2.7/site-packages
/usr/lib64/python2.7/site-packages
/usr/lib64/python2.7/site-packages/gtk-2.0
/usr/lib/python2.7/site-packages
sys.path = [
'/home/wangbm',
'/usr/local/Python3.7/lib/python37.zip',
'/usr/local/Python3.7/lib/python3.7',
'/usr/local/Python3.7/lib/python3.7/lib-dynload',
'/home/wangbm/.local/lib/python3.7/site-packages',
'/usr/local/Python3.7/lib/python3.7/site-packages',
]
USER_BASE: '/home/wangbm/.local' (exists)
USER_SITE: (exists)
ENABLE_USER_SITE: True
list2 = range(4,6)
list3 = range(7,9)
for item1 in list1:
for item2 in list2:
for item3 in list3:
print(item1+item2+item3)
list1 = range(1,3)
list2 = range(4,6)
list3 = range(7,9)
for item1,item2,item3 in product(list1, list2, list3):
print(item1+item2+item3)
12
13
13
14
13
14
14
15
... print('hello, python', file=f, flush=True)
>>> exit
$ cat test.log
hello, python
import timeit
def run_sleep(second):
print(second)
time.sleep(second)
# 只用这一行
print(timeit.timeit(lambda :run_sleep(2), number=5))
2
2
10.020059824
maxsize:最多可以缓存多少个此函数的调用结果,如果为None,则无限制,设置为 2 的幂时,性能最佳; typed:若为 True,则不同参数类型的调用将分别缓存。
@lru_cache(None)
def add(x, y):
print("calculating: %s + %s" % (x, y))
return x + y
print(add(1, 2))
print(add(1, 2))
print(add(2, 3))
3
calculating: 2 + 3
5
if n < 2:
return n
return fib(n - 2) + fib(n - 1)
def fib(n):
if n < 2:
return n
return fib(n - 2) + fib(n - 1)
print(timeit.timeit(lambda :fib(40), number=1))
# output: 31.2725698948
from functools import lru_cache
@lru_cache(None)
def fib(n):
if n < 2:
return n
return fib(n - 2) + fib(n - 1)
print(timeit.timeit(lambda :fib(500), number=1))
# output: 0.0004921059880871326
如果程序是被你没有处理过的系统信号杀死的,那么注册的函数无法正常执行。 如果发生了严重的 Python 内部错误,你注册的函数无法正常执行。 如果你手动调用了os._exit,你注册的函数无法正常执行。
func myfunc {
fmt.Println("B")
}
func main {
defer myfunc
fmt.Println("A")
}
B
def callback:
print('B')
with contextlib.ExitStack as stack:
stack.callback(callback)
print('A')
B
with open("big_file.txt", "r") as fp:
content = fp.read
with open(filename, "r") as fp:
yield fp.readline
with open(filename, "r") as fp:
while True:
chunk = fp.read(block_size)
if not chunk:
break
yield chunk