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.
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 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have defined a two-variables function like
def f(x1, x2):
return x1 + x2
Now, I want to pass a series of x2 into function f without calling it, to create a series of new functions. How can I achieve that?
If I understand your question correctly, this sounds like a job for functools.partial:
def f(x1, x2):
return x1 + x2
from functools import partial
funcs = [partial(f, x2=i) for i in range(10)] # example use case
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
beginner here. I'm writing a script that has a few related functions and am wondering what the standards are for naming the variables within my functions. For example if I have function_1 and function_2 that both a take some sort of file, is it acceptable to name both variables file? I know it will work, but is that horrible coding practice or is it alright to do?
def function_1(file):
# Do something
return file
def function_2(file):
# Do something
return file
def main():
file_1 = function_1(file)
file_2 = function_2(file)
if __name__ == "__main__":
main()
Since the two are parameters within the scope of two different functions, it is completely fine to name them the same. However, since file is a builtin in Python, I would suggest to name it differently or to add an underscore at the end of the name.
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 4 years ago.
Improve this question
I understand the technical definition of python closures: let's make it concrete.
def foo(x):
def bar(y):
print(x+y)
return bar
In this example x will get bound with bar. But what are these things actually good for? ie in the toy example above one could have just as easily written
def bar(x,y):
print(x+y)
I would like to know the best use cases for using closures instead of, for example, adding extra arguments to a function.
I think the most used example of closure is for caching functions with a decorator.
def cache_decorator(f):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = f(*args)
return cache[args]
return wrapper
#cache_decorator
def some_function(*args):
...
This way the cache cannot be referenced from anywhere, since you do not want your users to tamper with it.