分享

Python小知识-debug用到的5个内建函数

 禁忌石 2022-03-18

以下 5 个函数对调试和故障排除代码很有用。

breakpoint

需要暂停代码的执行并进入 Python 命令提示符? 这时候你就需要断点

调用断点函数将使你进入 Python 调试器 pdb,然后来分析代码中出现的问题。

这个内置函数是在 Python 3.7 中添加的。在旧版本的 Python 上,可以使用 import pdb ;pdb.set_trace() 代替。

dir

dir 函数可用于两件事:

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

2、查看特定对象的所有属性列表

在这里我们可以看到我们的局部变量,在启动一个新的 Python shell 之后,然后在创建一个新变量 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 等以及许多用于运算符重载的方法。

vars

vars 函数是两个相关事物的混搭:检查 locals() 和测试对象的 __dict__ 属性。

当不带参数调用 vars 时,它等效于调用 locals() 内置函数(显示所有局部变量及其值的字典)。

>>> vars(){'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}

当使用参数调用它时,它会访问该对象上的 __dict__ 属性(在许多对象上,它表示所有实例属性的字典)。

>>> 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.'})

如果你曾经尝试使用 my_object.__dict__,你可以使用 vars 来代替。

我通常在使用 vars 之前找到 dir。

type

type 函数将告诉你传递给它的对象的类型。

类实例的类型就是类本身:

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

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

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

如果你曾经看到有人使用 __class__,请知道他们可以使用更高级别的类型函数:

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

type 函数有时在实际代码中很有用(尤其是具有继承和自定义字符串表示的面向对象的代码),但在调试时也很有用。

但请注意,在进行类型检查时,通常使用 isinstance 函数而不是 type(还要注意,我们在 Python 中倾向于不进行类型检查,因为我们更喜欢练习鸭子类型)。

help

如果你在交互式 Python shell(我通常称之为 Python REPL)中,可能使用断点调试代码,并且您想知道某个对象、方法或属性是如何工作的,那么help()功能就会出现 便利。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多