zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    登录认证,全选,反选
    jQuery基础知识
    jQuery
    js练习
    BOM DOM
    mysql视图
    用CrwalSpider爬取boss直聘
    设置piplines.py数据管道
    在middlewares.py文件里添加代理ip
    爬取豆瓣电影
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/14887640.html
Copyright © 2011-2022 走看看