共计 4010 个字符,预计需要花费 11 分钟才能阅读完成。
大家好,我是陈程。
明天我将分享 15 个 Python 坏习惯,呈现这些坏习惯的起因次要是开发者在 Python 方面经验不足。通过摒弃这些习惯并以 Pythonic 的形式编写代码,不仅能够进步你的代码品质,还能够给看代码的人留下好印象哦~
1、拼接字符串用 + 号
坏的做法:
def manual_str_formatting(name, subscribers):
if subscribers > 100000: print("Wow" + name + "! you have" + str(subscribers) + "subscribers!") else: print("Lol" + name + "that's not many subs")
调整后的做法是应用 f-string,而且效率会更高:
def manual_str_formatting(name, subscribers):
# better if subscribers > 100000: print(f"Wow {name}! you have {subscribers} subscribers!") else: print(f"Lol {name} that's not many subs")
2、应用 finaly 而不是上下文管理器
坏的做法:
def finally_instead_of_context_manager(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((host, port)) s.sendall(b'Hello, world') finally: s.close()
调整后的做法是应用上下文管理器,即便产生异样,也会敞开 socket::
def finally_instead_of_context_manager(host, port):
# close even if exception with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) s.sendall(b'Hello, world')
3、尝试手动敞开文件
坏的做法:
def manually_calling_close_on_a_file(filename):
f = open(filename, "w") f.write("hello!\n") f.close()
调整后的做法是应用上下文管理器,即便产生异样,也会主动敞开文件,但凡有上下文管理器的,都应该首先采纳:
def manually_calling_close_on_a_file(filename):
with open(filename) as f: f.write("hello!\n") # close automatic, even if exception
4、except 前面什么也不写
坏的做法:
def bare_except():
while True: try: s = input("Input a number:") x = int(s) break except: # oops! can't CTRL-C to exit print("Not a number, try again")
这样会捕获所有异样,导致按下 CTRL-C 程序都不会终止,调整后的做法是
def bare_except():
while True: try: s = input("Input a number:") x = int(s) break except Exception: # 比这更好的是用 ValueError print("Not a number, try again")
5、函数参数应用可变对象
如果函数参数应用可变对象,那么下次调用时可能会产生非预期后果,坏的做法
def mutable_default_arguments():
def append(n, l=[]): l.append(n) return l l1 = append(0) # [0] l2 = append(1) # [0, 1]
调整后的做法,如下:
def mutable_default_arguments():
def append(n, l=None): if l is None: l = [] l.append(n) return l l1 = append(0) # [0] l2 = append(1) # [1]
6、从不必推导式
坏的做法
squares = {}
for i in range(10):squares[i] = i * i
调整后的做法
odd_squares = {i: i * i for i in range(10)}
7、推导式用的上瘾
推导式尽管好用,然而不能够就义可读性,坏的做法
c = [
sum(a[n * i + k] * b[n * k + j] for k in range(n)) for i in range(n) for j in range(n)
]
调整后的做法,如下:
c = []
for i in range(n):for j in range(n): ij_entry = sum(a[n * i + k] * b[n * k + j] for k in range(n)) c.append(ij_entry)
8、用 == 判断是否单例
坏的做法
def equality_for_singletons(x):
if x == None: pass if x == True: pass if x == False: pass
调整后的做法,如下:
def equality_for_singletons(x):
# better if x is None: pass if x is True: pass if x is False: pass
9、应用类 C 格调的 for 循环
坏的做法
def range_len_pattern():
a = [1, 2, 3] for i in range(len(a)): v = a[i] ... b = [4, 5, 6] for i in range(len(b)): av = a[i] bv = b[i]
...
调整后的做法,如下:
def range_len_pattern():
a = [1, 2, 3] # instead for v in a: ... # or if you wanted the index for i, v in enumerate(a): ... # instead use zip for av, bv in zip(a, b): ...
10、不实用 dict.items
坏的做法
def not_using_dict_items():
d = {"a": 1, "b": 2, "c": 3} for key in d: val = d[key] ...
调整后的做法,如下:
def not_using_dict_items():
d = {"a": 1, "b": 2, "c": 3} for key, val in d.items(): ...
11、应用 time.time() 统计耗时
坏的做法
def timing_with_time():
start = time.time() time.sleep(1) end = time.time() print(end - start)
调整后的做法是应用 time.perf_counter(),更准确:
def timing_with_time():
# more accuratestart = time.perf_counter() time.sleep(1) end = time.perf_counter() print(end - start)
12、记录日志应用 print 而不是 logging
坏的做法
def print_vs_logging():
print("debug info") print("just some info") print("bad error")
调整后的做法,如下:
def print_vs_logging():
# versus # in main level = logging.DEBUG fmt = '[%(levelname)s] %(asctime)s - %(message)s' logging.basicConfig(level=level, format=fmt) # wherever logging.debug("debug info") logging.info("just some info") logging.error("uh oh :(")
13、调用外部命令时应用 shell=True
坏的做法
subprocess.run([“ls -l”], capture_output=True, shell=True)
如果 shell=True,则将 ls -l 传递给 /bin/sh(shell) 而不是 Unix 上的 ls 程序,会导致 subprocess 产生一个两头 shell 过程,换句话说,应用两头 shell 意味着在命令运行之前,命令字符串中的变量、glob 模式和其余非凡的 shell 性能都会被预处理。比方,$HOME 会在在执行 echo 命令之前被解决解决。
调整后的做法是回绝从 shell 执行,如下:
subprocess.run([“ls”, “-l”], capture_output=True)
14、从不尝试应用 numpy
坏的做法
def not_using_numpy_pandas():
x = list(range(100)) y = list(range(100)) s = [a + b for a, b in zip(x, y)]
调整后的的做法,如下:
import numpy as np
def not_using_numpy_pandas():# 性能更快 x = np.arange(100) y = np.arange(100) s = x + y
15、喜爱 import *
调整后的做法,如下:
from itertools import *
count()
这样的话,没有人晓得这个脚本到底有少数变量,比拟好的做法:
from mypackage.nearby_module import awesome_function
def main():
awesome_function()
if name == ‘__main__’:main()
以上就是我总结的一些小技巧,到这里就完结了
若是有小伙伴还有其余补充或者不同意见,欢送在评论中进行探讨或者私信我哦~