简体   繁体   English

从另一个函数访问函数内部的变量

[英]Accessing a variable inside of a function, from another function

I have function AB which has 1 parameter, and AB has a bunch of loops and I have a counter which keeps track of how much it loops. 我有一个具有1个参数的函数AB,AB具有一堆循环,并且我有一个计数器跟踪其循环了多少。 It (must) returns an either True or False answer. 它(必须)返回对或错的答案。

I have a second function called AC which calls upon 2 instances of AB with different things for the parameters and compares them like so 我有一个名为AC的第二个函数,该函数调用具有不同内容的AB的2个实例作为参数,并像这样比较它们

if (AB(option1) == True and AB(option2) == False):
     result = "Option1 Wins"
elif (AB(option1) == False and AB(option2) == True):
     result = "Option2 Wins"
if (AB(option1) == True and AB(option2) == True):
     result = ??

however if both cases are 'True', I need to know which case reached 'True' first, so this means I would need to know how many times it loops (i need to know the value of the counter variable) 但是,如果两种情况均为“ True”,则我需要先知道哪种情况达到“ True”,所以这意味着我需要知道循环了多少次(我需要知道计数器变量的值)

How can I access the variable? 如何访问变量?

I was thinking of making a helper function to access it but I'm not sure if that's possible/how to do that 我当时正在考虑制作一个辅助函数来访问它,但是我不确定这是否可行/如何实现

You can return more than one value from a function in python: 您可以从python中的一个函数返回多个值:

def AB(param):
   return True, 1

val = AB(1) # it will be (True,1), so it's a set
val, val1 = AB(2) # val = True, val1 = 1

So in your code you should check AB(param)[0] to check True, if you want to keep only one return value (you should get them in different variables). 因此,在代码中,如果您只想保留一个返回值(应将它们放入不同的变量中),则应检查AB(param)[0]以检查True。

But your code is wrong, since you are calling AB() each time you are checking the output, which will execute all the loops and, eventually, could bring an unexpected response for the same input. 但是您的代码是错误的,因为每次检查输出时都要调用AB(),这将执行所有循环,最终可能为相同的输入带来意外的响应。 You should get the output from AB() for each param before, and then check them in your if block. 您应该先从AB()获取每个参数的输出,然后在if块中检查它们。

Zero evaluates to False and non-zero evaluates to True : 零的结果为False ,非零的结果为True

If AB fails it returns 0 otherwise it returns the amount of loops: 如果AB失败,则返回0否则返回循环次数:

def AB(param):
    if failed:
        return 0
    else:
        return counter

Usage: 用法:

if AB(option1) and not AB(option2):
    result = "Option1 Wins"
elif not AB(option1) and AB(option2):
    result = "Option2 Wins"
elif AB(option1) < AB(option2):
    result = "Option1 Wins"
else:
    result = "Option2 Wins"

OOP solution: Create a class with this methods as member function and the counter variable as a member variable. OOP解决方案:创建一个使用此方法作为成员函数并将counter变量作为成员变量的类。 Now you can check that value from anywhere for any object of that class. 现在,您可以从任何地方检查该类的任何对象的值。

Also in your code, no need to invoke AB multiple times. 同样在您的代码中,无需多次调用AB

o1 = AB(option1)
o2 = AB(option2)
if o1 and not o2:
     result = "Option1 Wins"
elif not o1 and o2):
     result = "Option2 Wins"
elif o1 and o2:
     result = ??

An object oriented solution might look like following (code not tested) 面向对象的解决方案可能类似于以下内容(未测试代码)

class solver(object, option):
    reached = 0
    option = option
    status = False

    def AB(self):
        # implementation. use self.option and update self.reached and self.status

opt1 = solver(option1)
opt2 = solver(option2)
o1 = opt1.AB()
o2 = opt2.AB()

if o1.status and not o2.status:
     result = "Option1 Wins"
elif not o1.status and o2.status):
     result = "Option2 Wins"
elif o1 and o2:
     if o1.reached < o2.reached:
         # o1 reached first

You can use global variables to store values of the counters. 您可以使用全局变量来存储计数器的值。 See http://www.diveintopython.net/html_processing/locals_and_globals.html for more details. 有关更多详细信息,请参见http://www.diveintopython.net/html_processing/locals_and_globals.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM