Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I searched the whole internet for the meaning of the return statement.
I know it ends the define statement, but I know it still does something else!
What else does it do?
It returns the flow of control to the calling function. It also returns output/results to the calling function.
Consider the function below:
def am_i_wrong(answer):
if answer == 'yes':
return True
else:
return False
You have multiple returns. So return doesn't simply end the function definition. It instead is the point at which the function returns the result to the caller.
If answer is equal to 'yes' then anything after the if statement (after if and else) is never run because the function has already returned.
The docs explain fully how a return function works. Its also the first answer in the google query python return...
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
Help! Why does Out[14] print True although i placed return False at the end of the function has_33(nums)?
Because the 'return' keyword ends the function on the spot.
'Return' keyword exits a function so in your case the 'return False' does not even run.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi want to return value after for loop. My code is -
def asd():
data.append("a")
data.append("b")
for i in range(0,2):
value = data[i]
return value
I am expecting to return both a and b when calling the function but it is returning only b. Is there any other method. Thanks in advance
Unless you have a very pressing reason for doing things this way, this example can be simplified to
def asd():
return 'a', 'b'
This is not an answer, but it will help you to find your mistake.
The = is an assignment operator. The value provided behind the = is assigned to the variable in front of it, e.g. a = 3 means that a now carries the value 3.
Now two easy questions
a=3
a=5
print a
Which value gets printed?
Similarly,
for i in range(100):
a = i
print a
What value gets printed?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What happens to variables assigned for the first time (defined) inside if statement or for loop if long time passed from when their code run. Is there some sort of garbage collection that may result in a not defined variable exception. For example:
if True:
a=1
else:
a=3
# long time passed and other codes run
# .
# .
# .
print (a)
I encountered an error in my code that I suspect this to be the reason. Is it documented somewhere in official Python documentation ?
In Python, if you define a variable within an if statement, it will continue to exist after the if statement concludes. Scopes are defined for a class, a def, or the global scope; if you're in a function and you define a variable inside if, for example, that variable will exist until the function is done executing.
Be careful, however, of defining variables in code like this:
if x == True:
a = 1
else:
print "Not true"
If you have code like this, and x ends up being False, then a will not be defined. Later calls to a will throw an exception as a result. So make sure that you get rid of any potential problems of that sort.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In the following code I keep getting the response that object of NoneType has no len(), but there is no length function anywhere in the code- does anyone know what is wrong?
def constant_pension(salary, save, growth_rate, years):
if salary<0 or save<0 or save>100 or growth_rate<=-100 or years<=0: #invalid
return(None)
i=0
fund_list=[]
old_fund=0
new_fund=0
while i<years:
new_fund=old_fund*(1+growth_rate*.01)+salary*save*.01
fund_list.append(new_fund)
old_fund=new_fund
i=i+1
return(fund_list)
pass
I can only guess since you haven't provided the traceback, but it looks like where you call the constant_pension function is probably something like:
funds = constant_pension(salary_rate, savings, growth, len(retirement))
and retirement is None. (The names are probably all wrong, but hopefully you get the idea.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I just want to know which way is more preferable in python.
Imagine two functions:
1 function:
def foo(key):
if bar.has_key(key):
return bar.get(key)
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
2 function:
def foo(key):
if bar.has_key(key):
return bar.get(key)
else:
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
As you can see the only difference is else statement. So the question is will it affect performance somehow. Or are there any reasons to include else in this type of functions?
If the choice is between those two approaches, I would pick the first one. return is pretty explicit that execution terminates at that point. I find if x { return y } else { ... } an anti-pattern for this reason (not just in Python -- I see this in C/C++ code and it irks me there, too).
If you are returning, an else block is entirely unnecessary and causes pointless indentation of a block of code that might be quite large. The more nested structures you have, the more difficult it is to maintain proper context in your head while reading code. For this reason I tend to prefer less nesting when it does not obfuscate logic, and in this case I don't think it would.
The pythonic way:
def foo(key):
return bar.get(key, something)
While this question is a little opinion based, I'd say the second is more Pythonic for the reason of "explicit is better than implicit". The second function is clearly saying "if this condition, do this. Otherwise, do this". On the other hand, the first function implies the "Otherwise" part.