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

來源:互聯網
上載者:User

標籤:__call   pack   form   getattr   直接   and   efault   red   get   

  直接見代碼:

 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 + y12     """13     return x + y14 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 + y11     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       : add14 func_code_filename:  D:/Users/14/PycharmProjects/ias/shapan/test_func.py

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.