1. python中常用的内置函数

1. vars

vars(objcet) 函数返回对象object的属性和属性值的字典对象

def test(a, b):
    # {'a': 10, 'b': 20}  常用打印函数的所有入参
    print(vars())
    return a + b


if __name__ == '__main__':
    test(10, 20)

 

返回对象object的属性和属性值的字典对象,如果没有参数,就打印当前调用位置的属性和属性值。

 

2.locals

def test(a, b):
    tmp = list()
    tmp.append(a)
    tmp.append(b)
    # {'a': 10, 'b': 20, 'tmp': [10, 20]}  # 获取函数中的入参及内部的所有局部变量
    print(locals())
    return a + b


if __name__ == '__main__':
    t = test(10, 20)

 

 

3. traceback.print_exc()

mport traceback


def test(a, b):
    c = 1 / 0
    # Traceback (most recent call last):
    #   File "/Users/xx/xx/xx-xx/debug技巧/c01常用的内置函数_vars.py", line 15, in <module>
    #     t = test(10, 20)
    #   File "/Users/xx/xx/xx-xx/debug技巧/c01常用的内置函数_vars.py", line 9, in test  # 精确定位到第几行
    #     c = 1 / 0
    # ZeroDivisionError: division by zero
    print(traceback.format_exc())
    return a + b


if __name__ == '__main__':
    t = test(10, 20)

 

 

 

 

 

 

# TODO

posted @ 2021-06-15 23:24  张京墨  阅读(126)  评论(0编辑  收藏  举报