1. 程式人生 > 其它 >1. python中常用的內建函式

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