Hello Mellow
Hello Mellow

Reputation: 169

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. 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

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)

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

Upvotes: 0

Views: 88

Answers (4)

Fallen
Fallen

Reputation: 4565

OOP solution: Create a class with this methods as member function and the counter variable as a member variable. 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.

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

Upvotes: 0

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24164

Zero evaluates to False and non-zero evaluates to True:

If AB fails it returns 0 otherwise it returns the amount of loops:

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"

Upvotes: 0

ozren1983
ozren1983

Reputation: 1961

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.

Upvotes: -1

Alberto
Alberto

Reputation: 727

You can return more than one value from a function in 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).

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. You should get the output from AB() for each param before, and then check them in your if block.

Upvotes: 2

Related Questions