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

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

        如何在函数中使用局部变量并返回它?

        How to use local variable in a function and return it?(如何在函数中使用局部变量并返回它?)

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

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

                  <tfoot id='JPOiT'></tfoot>

                  本文介绍了如何在函数中使用局部变量并返回它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个脚本来设置局部变量,从函数中引用它,并且可以将操作值返回到主范围(或任何它被称为的;我是 Python 新手)

                  I am trying to create a script that sets a local variable, references it from a function, and can return the manipulated value back to the main scope (or whatever it's called; I'm new to Python)

                  我已经简化了我的代码,以展示我想要完成的最基础的工作,我认为是将模块中的本地导入到功能块中.

                  I have simplified my code to show the utmost basics of what I am trying to accomplish, which is to import a local from the module into a function block, I think.

                  我已经通过使用 globals 来实现这一点,但这不是最好的解决方案...

                  I have gotten this to work by using globals, but that isn't the best solution . . .

                  chambersinreactor = 0;
                  cardsdiscarded = 0;
                  
                  def find_chamber_discard(): 
                      """Find chambers and discard in row (reads each player slot)"""
                      chambersinreactor = 0; # Resets the variable, not what I want
                      cardsdiscarded = 0; # Resets the variable, not what I want
                      chambersinreactor += 1
                      cardsdiscarded += 1
                      return # Don't know what to put here
                  
                  find_chamber_discard()
                  
                  print chambersinreactor # prints as 0, should be 1
                  print cardsdiscarded    # prints as 0, should be 1
                  

                  推荐答案

                  函数不应该知道它们被调用的范围;函数的重点是制作一个可重用的代码块,可以从不同的地方多次调用.

                  Functions shouldn't have to know what scope they're called from; the point of a function is to make a re-usable block of code that can be invoked multiple times from different places.

                  您通过函数的输入变量传递信息函数.该函数通过返回信息将信息传回给它的调用者.

                  You communicate information to a function by passing it through its input variables. The function communicates information back to its caller by returning it.

                  管理范围的变量是该范围内代码的工作而不是它调用的任何函数.如果您需要将变量设置为由函数确定的值,那么您可以让函数返回这些值并使用它们来设置变量.如果函数计算的值取决于调用范围内的变量值,那么您需要将它们作为参数传递给函数.你调用的函数不应该知道你正在使用什么变量,也不应该弄乱它们.

                  Managing the variables of a scope is the job of the code in that scope not any functions it invokes. If you need to set variables to values determined by a function, then you have the function return those values and you use them to set the variables. If the values the function is calculating depend on the values of variables you have in the calling scope, then you need to pass them to the function as arguments. The function you're calling shouldn't have to know what variables you're using, and shouldn't be able to mess with them.

                  把所有这些放在一起,你想做的是这样的:

                  Putting that all together, what you want to do is something like this:

                  def find_chamber_discard(chambersinreactor, cardsdiscarded):
                      chambersinreactor += 1
                      cardsdiscarded += 1
                      return (chambersinreactor, cardsdiscarded)
                  
                  chambersinreactor = 0;
                  cardsdiscarded = 0;
                  
                  chambersinreactor, cardsdiscarded = find_chamber_discard(chambersinreactor, cardsdiscarded)
                  
                  print chambersinreactor
                  print cardsdiscarded
                  

                  有一些方法可以通过全局变量或操纵可变数据结构来解决这个问题,但最终它们会降低您的程序的灵活性,并且更有可能包含难以发现的错误.这些技术都有一席之地,但您在函数之间进行信息交流的第一种方法确实应该是传递参数和接收返回值.

                  There are ways to get around this with global variables or manipulating mutable data structures, but ultimately they make your program less flexible and more likely to contain errors that will be difficult to spot. There is a place for those techniques, but the first method you reach for to communicate information to and from functions really should be passing arguments and receiving return values.

                  这篇关于如何在函数中使用局部变量并返回它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='ghJVf'></tfoot>

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

                      • <bdo id='ghJVf'></bdo><ul id='ghJVf'></ul>

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

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