• <legend id='4v8NF'><style id='4v8NF'><dir id='4v8NF'><q id='4v8NF'></q></dir></style></legend>
    <tfoot id='4v8NF'></tfoot>

        <small id='4v8NF'></small><noframes id='4v8NF'>

        <i id='4v8NF'><tr id='4v8NF'><dt id='4v8NF'><q id='4v8NF'><span id='4v8NF'><b id='4v8NF'><form id='4v8NF'><ins id='4v8NF'></ins><ul id='4v8NF'></ul><sub id='4v8NF'></sub></form><legend id='4v8NF'></legend><bdo id='4v8NF'><pre id='4v8NF'><center id='4v8NF'></center></pre></bdo></b><th id='4v8NF'></th></span></q></dt></tr></i><div id='4v8NF'><tfoot id='4v8NF'></tfoot><dl id='4v8NF'><fieldset id='4v8NF'></fieldset></dl></div>

        • <bdo id='4v8NF'></bdo><ul id='4v8NF'></ul>

        打印 Python 函数中键入的参数名称

        时间:2023-09-28

          <bdo id='qQiuF'></bdo><ul id='qQiuF'></ul>
        • <small id='qQiuF'></small><noframes id='qQiuF'>

            <tbody id='qQiuF'></tbody>

          • <i id='qQiuF'><tr id='qQiuF'><dt id='qQiuF'><q id='qQiuF'><span id='qQiuF'><b id='qQiuF'><form id='qQiuF'><ins id='qQiuF'></ins><ul id='qQiuF'></ul><sub id='qQiuF'></sub></form><legend id='qQiuF'></legend><bdo id='qQiuF'><pre id='qQiuF'><center id='qQiuF'></center></pre></bdo></b><th id='qQiuF'></th></span></q></dt></tr></i><div id='qQiuF'><tfoot id='qQiuF'></tfoot><dl id='qQiuF'><fieldset id='qQiuF'></fieldset></dl></div>

                <tfoot id='qQiuF'></tfoot>

                1. <legend id='qQiuF'><style id='qQiuF'><dir id='qQiuF'><q id='qQiuF'></q></dir></style></legend>
                2. 本文介绍了打印 Python 函数中键入的参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有类似的东西

                  def myfunc(list):
                  

                  当我调用函数时,我可以输入类似

                  When I call the function, I can type like

                  myfunc(List1)
                  

                  有没有办法打印出函数中作为参数输入的列表?比如:

                  There is a way to print out the list typed as argument in the function? Something like:

                  def myfunc(list):
                      print (list.name)
                  

                  那会给出:

                  myfunc(List1)
                  List1
                  

                  提前谢谢你

                  推荐答案

                  一个变体 建议by omri-saadon,是使用 inspect.getframeinfo 其中列出了code_context.

                  A variant over the one suggested by omri-saadon, is to use inspect.getframeinfo which lists the code_context.

                  import inspect
                  
                  def debug(*args):
                  
                      try:  # find code_context
                  
                          # First try to use currentframe() (maybe not available all implementations)
                          frame = inspect.currentframe()
                          if frame:
                              # Found a frame, so get the info, and strip space from the code_context
                              code_context = inspect.getframeinfo(frame.f_back).code_context[0].strip()
                          else:
                  
                              # No frame, so use stack one level above us, and strip space around
                              # the 4th element, code_context
                              code_context = inspect.stack()[1][4][0].strip()
                  
                      finally:
                           # Deterministic free references to the frame, to be on the safe side
                           del frame
                  
                      print('Code context: {}'.format(code_context))
                      print('Actual arguments: {}'.format(args))
                  
                  
                  ## Test code to have something to output #########
                  def my_seventh():
                     return 7
                  
                  a = 0.2
                  b = 1.2
                  c = a + 1
                  my_eight = my_seventh
                  
                  debug(a, b, c, b+2)
                  debug([4.1, 4.2], (5.1, 5.2), {6.1: 6.2}, my_seventh, my_eight, my_eight())
                  
                  my_list = [1, 2, 3, 4, 5]
                  
                  def first_level():
                      def second_level():
                           debug(my_list[:2],
                                 my_list[3:])
                  
                      second_level()
                  
                  first_level()
                  

                  运行这段代码时的输出是:

                  Output when running this code is:

                  Code context: debug(a, b, c, b+2)
                  Actual arguments: (0.2, 1.2, 1.2, 3.2)
                  Code context: debug([4.1, 4.2], (5.1, 5.2), {6.1: 6.2}, my_seventh, my_eight, my_eight())
                  Actual arguments: ([4.1, 4.2], (5.1, 5.2), {6.1: 6.2}, <function my_seventh at 0x102294488>, <function my_seventh at 0x102294488>, 7)
                  Code context: my_list[3:])
                  Actual arguments: ([1, 2], [4, 5])
                  

                  换句话说,使用检查选项获取 frameinfo 将为您提供触发函数调用的源代码行(或最后一行).但是,如果实际调用被拆分为多行,则只有最后一行作为代码上下文给出.到目前为止,我还没有找到将代码上下文中的命名变量连接到实际参数的方法.

                  In other words, using the inspect options to get the frameinfo will give you the source line (or last line thereof) which triggered your function call. However if the actual call is split over multiple lines, only the last line is given as code context. And so far I've not found a way to connect the named variables in the code context to the actual arguments.

                  这篇关于打印 Python 函数中键入的参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:*作为python中的参数是什么意思? 下一篇:仅使用传递的参数子集创建 namedtuple 对象

                  相关文章

                  <small id='gcsjb'></small><noframes id='gcsjb'>

                    • <bdo id='gcsjb'></bdo><ul id='gcsjb'></ul>
                  1. <legend id='gcsjb'><style id='gcsjb'><dir id='gcsjb'><q id='gcsjb'></q></dir></style></legend><tfoot id='gcsjb'></tfoot>

                    1. <i id='gcsjb'><tr id='gcsjb'><dt id='gcsjb'><q id='gcsjb'><span id='gcsjb'><b id='gcsjb'><form id='gcsjb'><ins id='gcsjb'></ins><ul id='gcsjb'></ul><sub id='gcsjb'></sub></form><legend id='gcsjb'></legend><bdo id='gcsjb'><pre id='gcsjb'><center id='gcsjb'></center></pre></bdo></b><th id='gcsjb'></th></span></q></dt></tr></i><div id='gcsjb'><tfoot id='gcsjb'></tfoot><dl id='gcsjb'><fieldset id='gcsjb'></fieldset></dl></div>