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.
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
How to return a local variable from a function? But there is a way to access the local variables of a function using pointers, by creating another pointer variable that points to the variable to be returned and returning the pointer variable itself.
To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.
The local variable may be returned to calling scope (the point in the program where the function was called), or it may be logged and discarded when the function ends. We'll be done logging once we move on from this learning track, so practice using return when the exercise doesn't specify what to use.
You can use local to make a variable local to a function. When a variable is made local, it inherits the initial value and exported and read-only attributes from the variable with the same name in the surrounding scope, if there is one. Otherwise, the variable is initially unset.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With