Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to break out or restart a function in python 2. Putting a return statement should stop all execution. Same goes for restarting the function.
def function():
...
if len(lst) == 1:
return value
print 'foo'
else:
function()
print 'foo'
In this case 'foo' would be printed twice.
The return statement only stops the execution of the current instance of the function.
Since you're doing a recursion, you will still get the other calls of that function running until they hit their own return (if any).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
def factorial(n):
if (n>1):
return n* factorial(n-1)
def factorial(n):
if (n>1):
return n* factorial(n-1)
else:
return 1
The first code has an error but the second function runs properly
what is the difference between the two programs?
what is the problem with the first code?
thank you
The problem is that when the first code evaluates for n<=1, there is no returned value. This causes the function to (by default) return None. Now while evaluating for 2, the program says
return n* factorial(n-1)
That is equivalent to;
return 2*None #Since factorial(1)=None
Hence It gives an error as NoneType cannot be multiplied by an integer
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 2 years ago.
Improve this question
Suppose I have two functions
def sum(a, b):
result = a + b
return result
and
def sum(a, b):
return a + b
From the point of view of good programming practices and software engineering, which solution is better? Return a value or assigning a value to a variable and then return? Why?
2nd option is good because it saves space of a variable but at times, we use more variables on purpose for the sake of clarity. We always have to maintain a good balance between clarity and space.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Now I am watching PY4E youtube, the difference between void and fruitial function does not touch me well. just return value?
Also, if the void function has a function PRINT(SOMETHING)
even though it has not return value, but it has some value or result?
isn't it? I am little bit confused.
In Python, all functions return something, but some return None, which is ignored. These are called "void". Functions that return anything else are called "fruitful".
For example, list.append():
>>> a = []
>>> a.append(1)
Note how nothing was printed after a.append(). But it did actually return None, which we can confirm by printing its return value:
>>> print(a.append(2))
None
print as well returns none:
>>> print(print(a.append(3)))
None
None
And user-defined functions which don't have a return statement return None.
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 am using flask, and some functions have decorators, to check the presence of some headers and return some error codes if they are missing.
In these decorators, before returning the decorated function i do something like
decorated_function.__doc__ += "Returns 400 if the X-Version header is not present."
Is this pythonic? Is there a better way to achieve it?
I am using wraps already from functools.
def ModDoc(doc):
def wrapped(func):
func.__doc__ = doc
return func
return wrapped
#ModDoc("test2")
def test():
"""test"""
return
print test.__doc__
Will modify the docstring of anything it is applied too. Remember these changes are purely interactive, and will not show up in stored or auto generated documentation.
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 am attempting to implement an interpreter for brainfuck and as of now, I am just using a series of if/elif statements.
if(i == ">"):
...
elif(i == "<"):
...
elif(i == "+"):
...
elif(i == "-"):
...
However, this seems very clunky and un-pythonic to me. Is there a better (cleaner/faster/more aesthetically pleasing) way to implement this?
I have a quick implementation of a Brainfuck interpreter for Python in a GitHub repo. In a nutshell, though, you could keep a dictionary, where the keys are the Brainfuck characters and the values are function (or method) objects, and then dispatch on that. Something like this:
instructions = {
'+': increment,
'-': decrement,
# Other functions
}
def run(tape):
ch = next_token(tape)
if ch in instructions:
instructions[ch]()
(Not an actual implementation, just a quick illustration.)