• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python如何编写带输出参数的函数(通过引用调用)?

python 搞java代码 2个月前 (05-21) 2次浏览 已收录 0个评论

请记住在 Python 中参数是通过赋值来传递的。 由于赋值只是创建了对象的引用,因此在调用者和被调用者的参数名称之间没有别名,所以本身是没有按引用调用的。 你可以通过多种方式实现所需的效果。

  1. 通过返回一个结果元组:

    <span class="k">def</span> <span class="nf">func2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="n">a</span> <span class="o">=</span> <span class="s1">"new-value"</span>        <span class="c1"># a and b are local names</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">b</span> <span class="o">+</span> <span class="mi">1</span>              <span class="c1"># assigned to new objects</span>
        <span class="k">return</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>            <span class="c1"># return new values</span>
    
    <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="s1">"old-value"</span><span class="p">,</span> <span class="mi">99</span>
    <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">func2</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>                <span class="c1"># output: new-value 100</span>
    
    www#gaodaima.com来源gaodai.ma#com搞#代!码网搞代码

    这几乎总是最清晰明了的解决方案。

  2. 通过使用全局变量。 这种方式不是线程安全的,而且也不受推荐。

  3. 通过传递一个可变 (即可原地修改的) 对象:

    <span class="k">def</span> <span class="nf">func1</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
        <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">"new-value"</span>     <span class="c1"># "a" references a mutable list</span>
        <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>        <span class="c1"># changes a shared object</span>
    
    <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="s1">"old-value"</span><span class="p">,</span> <span class="mi">99</span><span class="p">]</span>
    <span class="n">func1</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>    <span class="c1"># output: new-value 100</span>
    
  4. 通过传递一个会被改变的字典:

    <span class="k">def</span> <span class="nf">func3</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">args</span><span class="p">[</span><span class="s1">"a"</span><span class="p">]</span> <span class="o">=</span> <span class="s1">"new-value"</span>     <span class="c1"># args is a mutable dictionary</span>
        <span class="n">args</span><span class="p">[</span><span class="s1">"b"</span><span class="p">]</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="s1">"b"</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>   <span class="c1"># change it in-place</span>
    
    <span class="n">args</span> <span class="o">=</span> <span class="p">{</span><span class="s1">"a"</span><span class="p">:</span> <span class="s1">"old-value"</span><span class="p">,</span> <span class="s1">"b"</span><span class="p">:</span> <span class="mi">99</span><span class="p">}</span>
    <span class="n">func3</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="s1">"a"</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="s1">"b"</span><span class="p">])</span>
    
  5. 或者在一个类实例中捆绑值:

    <span class="k">class</span> <span class="nc">callByRef</span><span class="p">:</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">**</span><span class="n">args</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">args</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    
    <span class="k">def</span> <span class="nf">func4</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">args</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s1">"new-value"</span>        <span class="c1"># args is a mutable callByRef</span>
        <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span>         <span class="c1"># change object in-place</span>
    
    <span class="n">args</span> <span class="o">=</span> <span class="n">callByRef</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="s1">"old-value"</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">99</span><span class="p">)</span>
    <span class="n">func4</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
    

    几乎没有任何适当理由将问题如此复杂化。

你的最佳选择是返回一个包含多个结果的元组。

来源:搞代码网:原文地址:https://www.gaodaima.com


喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址