1. 程式人生 > >python 函數的幾個屬性 func_name, func_code等

python 函數的幾個屬性 func_name, func_code等

pytho test cts als size attribute odin put lob

  直接見代碼:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2018/07/25 10:14
 4 
 5 
 6 def add(x=0, y=1):
 7     """
 8     add input args x and y.
 9     :param x:
10     :param y:
11     :return: x + y
12     """
13     return x + y
14 
15 
16 if __name__ == __main__:
17     print dir(add)
18 attr_name_list = [ 19 func_closure, func_code, 20 func_defaults, func_dict, 21 func_doc, func_globals, 22 func_name 23 ] 24 for attr_name in attr_name_list: 25 print attr_name.ljust(15), :, getattr(add, attr_name) 26 print func_code_filename:
, add.func_code.co_filename

結果如下:獲取函數名稱和文件位置:func_name, func_code.co_filename。

 1 [‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__doc__‘, ‘__format__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__name__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘func_closure‘, ‘func_code‘, ‘func_defaults‘, ‘func_dict‘, ‘func_doc‘, ‘func_globals‘, ‘func_name‘]
2 func_closure : None 3 func_code : <code object add at 0000000001CEFE30, file "D:/Users/14/PycharmProjects/ias/shapan/test_func.py", line 6> 4 func_defaults : (0, 1) 5 func_dict : {} 6 func_doc : 7 add input args x and y. 8 :param x: 9 :param y: 10 :return: x + y 11 12 func_globals : {‘__builtins__‘: <module ‘__builtin__‘ (built-in)>, ‘__file__‘: ‘D:/Users/14/PycharmProjects/ias/shapan/test_func.py‘, ‘attr_name‘: ‘func_globals‘, ‘__package__‘: None, ‘add‘: <function add at 0x0000000002615AC8>, ‘attr_name_list‘: [‘func_closure‘, ‘func_code‘, ‘func_defaults‘, ‘func_dict‘, ‘func_doc‘, ‘func_globals‘, ‘func_name‘], ‘__name__‘: ‘__main__‘, ‘__doc__‘: None} 13 func_name : add 14 func_code_filename: D:/Users/14/PycharmProjects/ias/shapan/test_func.py

python 函數的幾個屬性 func_name, func_code等