<bdo id='ACjAC'></bdo><ul id='ACjAC'></ul>
  1. <small id='ACjAC'></small><noframes id='ACjAC'>

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

      python的可变长度参数(*args)是否在函数调用时扩展生成器?

      Do python#39;s variable length arguments (*args) expand a generator at function call time?(python的可变长度参数(*args)是否在函数调用时扩展生成器?)
    2. <i id='epFS8'><tr id='epFS8'><dt id='epFS8'><q id='epFS8'><span id='epFS8'><b id='epFS8'><form id='epFS8'><ins id='epFS8'></ins><ul id='epFS8'></ul><sub id='epFS8'></sub></form><legend id='epFS8'></legend><bdo id='epFS8'><pre id='epFS8'><center id='epFS8'></center></pre></bdo></b><th id='epFS8'></th></span></q></dt></tr></i><div id='epFS8'><tfoot id='epFS8'></tfoot><dl id='epFS8'><fieldset id='epFS8'></fieldset></dl></div>
            <bdo id='epFS8'></bdo><ul id='epFS8'></ul>

            1. <tfoot id='epFS8'></tfoot>
                  <tbody id='epFS8'></tbody>

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

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

                本文介绍了python的可变长度参数(*args)是否在函数调用时扩展生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                考虑以下 Python 代码:

                Consider the following Python code:

                def f(*args):
                    for a in args:
                        pass
                
                foo = ['foo', 'bar', 'baz']
                
                # Python generator expressions FTW
                gen = (f for f in foo)
                
                f(*gen)
                

                *args 会在调用时自动扩展生成器吗?换句话说,我是否在 f(*gen) 内对 gen 进行了两次迭代,一次是展开 *args,一次是对 args 进行迭代?还是生成器保持原始状态,而迭代只在 for 循环中发生一次?

                Does *args automatically expand the generator at call-time? Put another way, am I iterating over gen twice within f(*gen), once to expand *args and once to iterate over args? Or is the generator preserved in pristine condition, while iteration only happens once during the for loop?

                推荐答案

                生成器在函数调用时展开,您可以轻松查看:

                The generator is expanded at the time of the function call, as you can easily check:

                def f(*args):
                    print(args)
                foo = ['foo', 'bar', 'baz']
                gen = (f for f in foo)
                f(*gen)
                

                将打印

                ('foo', 'bar', 'baz')
                

                这篇关于python的可变长度参数(*args)是否在函数调用时扩展生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

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

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

                          <legend id='d0PnY'><style id='d0PnY'><dir id='d0PnY'><q id='d0PnY'></q></dir></style></legend>
                            <tbody id='d0PnY'></tbody>