问题描述
我正在尝试创建一个脚本来设置局部变量,从函数中引用它,并且可以将操作值返回到主范围(或任何它被称为的;我是 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.
这篇关于如何在函数中使用局部变量并返回它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!