以下 5 個(gè)函數(shù)對(duì)調(diào)試和故障排除代碼很有用。
需要暫停代碼的執(zhí)行并進(jìn)入 Python 命令提示符? 這時(shí)候你就需要斷點(diǎn)
調(diào)用斷點(diǎn)函數(shù)將使你進(jìn)入 Python 調(diào)試器 pdb,然后來分析代碼中出現(xiàn)的問題。
這個(gè)內(nèi)置函數(shù)是在 Python 3.7 中添加的。在舊版本的 Python 上,可以使用 import pdb ;pdb.set_trace() 代替。
dir 函數(shù)可用于兩件事:
1、查看所有局部變量的列表
2、查看特定對(duì)象的所有屬性列表
在這里我們可以看到我們的局部變量,在啟動(dòng)一個(gè)新的 Python shell 之后,然后在創(chuàng)建一個(gè)新變量 x 之后:
>>> dir()['__annotations__', '__doc__', '__name__', '__package__']>>> x = [1, 2, 3, 4]>>> dir()['__annotations__', '__doc__', '__name__', '__package__', 'x']
如果我們將該 x 列表傳遞到 dir 中,我們可以看到它具有的所有屬性:
>>> dir(x)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
我們可以看到典型的列表方法、append、pop、remove 等以及許多用于運(yùn)算符重載的方法。
vars 函數(shù)是兩個(gè)相關(guān)事物的混搭:檢查 locals() 和測(cè)試對(duì)象的 __dict__ 屬性。
當(dāng)不帶參數(shù)調(diào)用 vars 時(shí),它等效于調(diào)用 locals() 內(nèi)置函數(shù)(顯示所有局部變量及其值的字典)。
>>> vars(){'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
當(dāng)使用參數(shù)調(diào)用它時(shí),它會(huì)訪問該對(duì)象上的 __dict__ 屬性(在許多對(duì)象上,它表示所有實(shí)例屬性的字典)。
>>> from itertools import chain>>> vars(chain)mappingproxy({'__getattribute__': <slot wrapper '__getattribute__' of 'itertools.chain' objects>, '__iter__': <slot wrapper '__iter__' of 'itertools.chain' objects>, '__next__': <slot wrapper '__next__' of 'itertools.chain' objects>, '__new__': <built-in method __new__ of type object at 0x5611ee76fac0>, 'from_iterable': <method 'from_iterable' of 'itertools.chain' objects>, '__reduce__': <method '__reduce__' of 'itertools.chain' objects>, '__setstate__': <method '__setstate__' of 'itertools.chain' objects>, '__doc__': 'chain(*iterables) --> chain object\n\nReturn a chain object whose .__next__() method returns elements from the\nfirst iterable until it is exhausted, then elements from the next\niterable, until all of the iterables are exhausted.'})
如果你曾經(jīng)嘗試使用 my_object.__dict__,你可以使用 vars 來代替。
我通常在使用 vars 之前找到 dir。
type 函數(shù)將告訴你傳遞給它的對(duì)象的類型。
類實(shí)例的類型就是類本身:
>>> x = [1, 2, 3]>>> type(x)<class 'list'>
類的類型是它的元類,通常是類型:
>>> type(list)<class 'type'>>>> type(type(x))<class 'type'>
如果你曾經(jīng)看到有人使用 __class__,請(qǐng)知道他們可以使用更高級(jí)別的類型函數(shù):
>>> x.__class__<class 'list'>>>> type(x)<class 'list'>
type 函數(shù)有時(shí)在實(shí)際代碼中很有用(尤其是具有繼承和自定義字符串表示的面向?qū)ο蟮拇a),但在調(diào)試時(shí)也很有用。
但請(qǐng)注意,在進(jìn)行類型檢查時(shí),通常使用 isinstance 函數(shù)而不是 type(還要注意,我們?cè)?Python 中傾向于不進(jìn)行類型檢查,因?yàn)槲覀兏矚g練習(xí)鴨子類型)。
如果你在交互式 Python shell(我通常稱之為 Python REPL)中,可能使用斷點(diǎn)調(diào)試代碼,并且您想知道某個(gè)對(duì)象、方法或?qū)傩允侨绾喂ぷ鞯?,那么help()功能就會(huì)出現(xiàn) 便利。
聯(lián)系客服