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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。