Reputation: 55
There's something I don't understand here when it comes to returning variables. For the sake of simplicity, I wrote a really basic thing to sum up the problem I'm having:
def apples():
dingo = 2
return dingo
def bananas(dingo):
print(dingo)
def main():
apples()
bananas(dingo)
main()
So I create 'dingo' in the 'apples' function. I return it. I use it as a parameter in 'bananas'. I call them both in main, so why do I get the error that 'dingo' is undefined? Also, something I'm unable to do is put dingo = apples()
inside the bananas function. I can't unpack it within the bananas function because I want to call them both in main individually. Is there any way to get around this without unpacking?
Upvotes: 4
Views: 150
Reputation: 61365
In your main
function definition change the code to look like the following
def main():
bananas(apples())
This way you avoid any temporary assignment of the return values
Upvotes: 0
Reputation: 11060
Just to clarify, there is nothing special about variables names between scopes:
The code given in the accepted answer is the same as this:
def apples():
dingo = 2
return dingo
def bananas(another_name):
print(another_name)
def main():
result = apples()
bananas(result)
main()
What you call the parameter to bananas
is irrelevant.
Upvotes: 0
Reputation: 53
def apples():
dingo = 2
return dingo
def bananas(dingo):
print(dingo)
def main():
apples()
bananas(dingo)
main()
The variable dingo
is a local variable for the function definition apples()
and its scope(lifetime) ends as soon as the function call is done with.That means the variable name dingo
doesn't hold any meaning as soon as the function is over.
apples()
will return dingo
no doubt but the function bananas()
knows nothing about dingo and neither does main()
since dingo
is a local variable and confined to apples()
For example:
def ret_val():
var=5
return var
main()
x=ret_val
print x
Output:
5
the return statement only returns the value in the variable and not the variable by name. so you should replace code by the following:
def apples():
dingo = 2
return dingo
def bananas(dingo):
print(dingo)
def main():
x=apples()#here x will store the value of dingo i.e 2
bananas(x)#the value of x will be passed
main()
Whenever you return something the return statement terminates the function irrespective of the place where it is in the code of the function(i.e. the last statement or in the middle)
def check(x):
if (x % 2==0):
return "even"
return "odd"
Once the control goes to return statement then the function ends and the other return statement is not executed.Hence the return is like a break statement for functions which also gives back a value to the calling function.
Upvotes: 0
Reputation: 32600
You get that error because you didn't assign the return value of apples()
to anything, especially not a variable named dingo
in the scope of main()
. This would work:
def apples():
dingo = 2
return dingo
def bananas(dingo):
print(dingo)
def main():
result = apples()
bananas(result)
main()
Notice how I named the variable result
- it doesn't have to be named the same as the argument of the bananas()
function - it just has to be passed in with the name you assigned it to.
def bananas(dingo)
basically means: Create a function called bananas
that takes exactly one argument. Inside bananas()
, refer to that argument as dingo
.
So whatever that argument is called in the scope where you call bananas()
is irrelevant.
Same for apples: You create a variable dingo
, assign it the value 2
and return it - what's actually returned is just the value (2
), it's up to you to assign that result to variable that may or may not be called the same.
Upvotes: 2
Reputation: 34017
what you've returned should be assigned to some variable, otherwise the return value gonna be lost.
I'm curious didn't you get NameError: global name 'dingo' is not defined
with your sample code?
In [38]: def apples():
...: dingo = 2
...: return dingo
...:
...: def bananas(dingo):
...: print(dingo)
...:
...: def main():
...: dingo=apples()
...: bananas(dingo)
...:
...: main()
2
Upvotes: 0