zoukankan      html  css  js  c++  java
  • python 函数的几个属性 func_name, func_code等

      直接见代码:

     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
  • 相关阅读:
    解决Cannot delete or update a parent row: a foreign key constraint fails的mysql报错
    zabbix4.2绘制网络拓扑图-添加链路速率
    zabbix 添加宏变量
    238_Product of Array Except Self
    122_Best Time to Buy and Sell Stock II
    260_Single Number III
    C# 比较时间问题
    226_Invert Binary Tree
    100_Same Tree
    283_Move Zeroes
  • 原文地址:https://www.cnblogs.com/tlz888/p/9365908.html
Copyright © 2011-2022 走看看