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 @   张京墨  阅读(127)  评论(0编辑  收藏  举报
编辑推荐:
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!
点击右上角即可分享
微信分享提示