日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Python小知識-debug用到的5個內(nèi)建函數(shù)

 禁忌石 2022-03-18

以下 5 個函數(shù)對調(diào)試和故障排除代碼很有用。

breakpoint

需要暫停代碼的執(zhí)行并進(jìn)入 Python 命令提示符? 這時候你就需要斷點(diǎn)

調(diào)用斷點(diǎn)函數(shù)將使你進(jìn)入 Python 調(diào)試器 pdb,然后來分析代碼中出現(xiàn)的問題。

這個內(nèi)置函數(shù)是在 Python 3.7 中添加的。在舊版本的 Python 上,可以使用 import pdb ;pdb.set_trace() 代替。

dir

dir 函數(shù)可用于兩件事:

1、查看所有局部變量的列表

2、查看特定對象的所有屬性列表

在這里我們可以看到我們的局部變量,在啟動一個新的 Python shell 之后,然后在創(chuàng)建一個新變量 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

vars 函數(shù)是兩個相關(guān)事物的混搭:檢查 locals() 和測試對象的 __dict__ 屬性。

當(dāng)不帶參數(shù)調(diào)用 vars 時,它等效于調(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)用它時,它會訪問該對象上的 __dict__ 屬性(在許多對象上,它表示所有實(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

type 函數(shù)將告訴你傳遞給它的對象的類型。

類實(shí)例的類型就是類本身:

>>> x = [1, 2, 3]>>> type(x)<class 'list'>

類的類型是它的元類,通常是類型:

>>> type(list)<class 'type'>>>> type(type(x))<class 'type'>

如果你曾經(jīng)看到有人使用 __class__,請知道他們可以使用更高級別的類型函數(shù):

>>> x.__class__<class 'list'>>>> type(x)<class 'list'>

type 函數(shù)有時在實(shí)際代碼中很有用(尤其是具有繼承和自定義字符串表示的面向?qū)ο蟮拇a),但在調(diào)試時也很有用。

但請注意,在進(jìn)行類型檢查時,通常使用 isinstance 函數(shù)而不是 type(還要注意,我們在 Python 中傾向于不進(jìn)行類型檢查,因?yàn)槲覀兏矚g練習(xí)鴨子類型)。

help

如果你在交互式 Python shell(我通常稱之為 Python REPL)中,可能使用斷點(diǎn)調(diào)試代碼,并且您想知道某個對象、方法或?qū)傩允侨绾喂ぷ鞯?,那么help()功能就會出現(xiàn) 便利。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多