欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

梳理總結(jié)Python開發(fā)中需要摒棄的18個(gè)壞習(xí)慣

 更新時(shí)間:2022年01月21日 14:51:26   作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘  
大家好,今天給大家分享 18 個(gè) Python 初學(xué)者常有的壞習(xí)慣,這些壞習(xí)慣不僅影響 Python 代碼的可讀性,而且 影響 Python 的運(yùn)行性能,摒棄這些壞習(xí)慣并以 Pythonic 的方式編寫代碼,提高的不僅僅是你的代碼質(zhì)量,也給閱讀代碼的人留下好印象

廢話不多說,我們開始學(xué)習(xí)吧!

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,而且效率會(huì)更高:

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()

好的做法是使用上下文管理器,即使發(fā)生異常,也會(huì)關(guān)閉 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、嘗試手動(dòng)關(guān)閉文件

壞的做法:

def manually_calling_close_on_a_file(filename):
    f = open(filename, "w")
    f.write("hello!\n")
    f.close()

好的做法是使用上下文管理器,即使發(fā)生異常,也會(huì)自動(dòng)關(guān)閉文件,凡是有上下文管理器的,都應(yīng)該首先采用:

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")

這樣會(huì)捕捉所有異常,導(dǎo)致按下 CTRL-C 程序都不會(huì)終止,好的做法是

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、函數(shù)參數(shù)使用可變對象

如果函數(shù)參數(shù)使用可變對象,那么下次調(diào)用時(shí)可能會(huì)產(chǎn)生非預(yù)期結(jié)果,壞的做法

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、從不用推導(dǎo)式

壞的做法

squares = {}
for i in range(10):
    squares[i] = i * i

好的做法

odd_squares = {i: i * i for i in range(10)}

7、推導(dǎo)式用的上癮

推導(dǎo)式雖然好用,但是不可以犧牲可讀性,壞的做法

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 checking_type_equality():
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(1, 2)

    if type(p) == tuple:
        print("it's a tuple")
    else:
        print("it's not a tuple")

好的做法

def checking_type_equality():
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(1, 2)

    # probably meant to check if is instance of tuple
    if isinstance(p, tuple):
        print("it's a tuple")
    else:
        print("it's not a tuple")

9、用 == 判斷是否單例

壞的做法

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

10、判斷一個(gè)變量用 bool(x)

壞的做法

def checking_bool_or_len(x):
    if bool(x):
        pass

    if len(x) != 0:
        pass

好的做法

def checking_bool_or_len(x):
    # usually equivalent to
    if x:
        pass

11、使用類 C 風(fēng)格的 for 循環(huán)

壞的做法

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):
        ...

12、不實(shí)用 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():
        ...

13、解包元組使用索引

壞的做法

mytuple = 1, 2
x = mytuple[0]
y = mytuple[1]

好的做法

mytuple = 1, 2
x, y = mytuple

14、使用 time.time() 統(tǒng)計(jì)耗時(shí)

壞的做法

def timing_with_time():
    start = time.time()
    time.sleep(1)
    end = time.time()
    print(end - start)

好的做法是使用 time.perf_counter(),更精確:

def timing_with_time():
   # more accurate
    start = time.perf_counter()
    time.sleep(1)
    end = time.perf_counter()
    print(end - start)

15、記錄日志使用 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 :(")

16、調(diào)用外部命令時(shí)使用 shell=True

壞的做法

subprocess.run(["ls -l"], capture_output=True, shell=True)

如果 shell=True,則將 ls -l 傳遞給/bin/sh(shell) 而不是 Unix 上的 ls 程序,會(huì)導(dǎo)致 subprocess 產(chǎn)生一個(gè)中間 shell 進(jìn)程, 換句話說,使用中間 shell 意味著在命令運(yùn)行之前,命令字符串中的變量、glob 模式和其他特殊的 shell 功能都會(huì)被預(yù)處理。比如,$HOME 會(huì)在在執(zhí)行 echo 命令之前被處理處理。

好的做法是拒絕從 shell 執(zhí)行:

subprocess.run(["ls", "-l"], capture_output=True)

17、從不嘗試使用 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

18、喜歡 import *

壞的做法

from itertools import *

count()

這樣的話,沒有人直到這個(gè)腳本到底有多數(shù)變量, 好的做法:

from mypackage.nearby_module import awesome_function

def main():
    awesome_function()

if __name__ == '__main__':
    main()

到此這篇關(guān)于梳理總結(jié)Python開發(fā)中需要摒棄的18個(gè)壞習(xí)慣的文章就介紹到這了,更多相關(guān)Python 壞習(xí)慣內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論