• <small id='sbrFY'></small><noframes id='sbrFY'>

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

        <legend id='sbrFY'><style id='sbrFY'><dir id='sbrFY'><q id='sbrFY'></q></dir></style></legend>
      1. <tfoot id='sbrFY'></tfoot>

        在函数的代码对象上使用 Python exec 时如何获取返回值?

        How do I get the return value when using Python exec on the code object of a function?(在函数的代码对象上使用 Python exec 时如何获取返回值?)

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

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

                    <tbody id='lZvI8'></tbody>

                  <legend id='lZvI8'><style id='lZvI8'><dir id='lZvI8'><q id='lZvI8'></q></dir></style></legend>

                • 本文介绍了在函数的代码对象上使用 Python exec 时如何获取返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  出于测试目的,我想直接执行在另一个函数内部定义的函数.

                  For testing purposes I want to directly execute a function defined inside of another function.

                  我可以通过父函数的代码(func_code)得到子函数的代码对象,但是当我执行它时,我没有得到返回值.

                  I can get to the code object of the child function, through the code (func_code) of the parent function, but when I exec it, i get no return value.

                  有没有办法从执行的代码中获取返回值?

                  Is there a way to get the return value from the exec'ed code?

                  推荐答案

                  是的,你需要在 exec 语句中进行赋值:

                  Yes, you need to have the assignment within the exec statement:

                  >>> def foo():
                  ...     return 5
                  ...
                  >>> exec("a = foo()")
                  >>> a
                  5
                  

                  这可能与您的情况无关,因为它被用于受控测试,但使用带有用户定义输入的 exec 时要小心.

                  This probably isn't relevant for your case since its being used in controlled testing, but be careful with using exec with user defined input.

                  这篇关于在函数的代码对象上使用 Python exec 时如何获取返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                • <tfoot id='fKsAs'></tfoot>
                  <legend id='fKsAs'><style id='fKsAs'><dir id='fKsAs'><q id='fKsAs'></q></dir></style></legend>

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

                        <tbody id='fKsAs'></tbody>

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