如何在函数中使用局部变量并将其返回? [英] How to use local variable in a function and return it?

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

问题描述

我正在尝试创建一个脚本,该脚本设置一个局部变量,从一个函数引用它,并且可以将操作后的值返回到主作用域(或任何所谓的作用域;我是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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆