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.
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 3 years ago.
Improve this question
x = 2
creates an integer variable.
y.append(3)
gives an error message (name 'y' is not defined).
In the first one a variable x can be assigned a value without first defining it, why can't we do the same thing with the lists (you have to first define it using l = []). Is this the result of a fundamental design choice in the Python language?
Thanks.
The actual act of assignment also creates the variable if it doesn't exist.
When you attempt to call a member function the variable needs to exist, so the Python interpreter knows what kind of variable it is (the type of the variable). If the interpreter doesn't know the kind of the variable, it can't know what member functions exist on the object.
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.
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 5 years ago.
Improve this question
I am new on Python.
I want to create a function with two vectors inside. I tried like this
def twovectors((velocity1,length1),(velocity2,length2)):
but I have a message error like
SyntaxError: invalid syntax.
Please, need help.
You cannot put tuple in the function definition as a parameter. Check Multiple Function Arguments or 8.6. Function definitions in the Python language reference.
Try something like this this:
def twovectors(vector1, vector2):
velocity1, length1 = vector1
velocity2, length2 = vector2
# Other code...
I used tuple unpacking to expand provided tuple arguments.
You write functions in python in this way :
def twovectors(velocity1, velocity2):
# You can get the length of those vectors after you get inside the function
len1, len2 = len(velocity1), len(velocity2)
// Your code here
return whateveryouwantto
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm probably missing something obvious here. With the following code:
class Thing():
def __init__(self, name):
self.name = name
that = Thing()
I get the error 'init needs two arguments'. I thought 'self' was one of the arguments and when I try to instantiate the object by putting the name inside the parentheses I get other errors.
Yes, your __init__ takes two arguments: self, and name. When you call Thing(), self is passed implicitly, but you still need to pass the second one explicitly, like Thing("name"). If you're still getting an error when doing that, that's a different story. You should post that error as well.
(And I doubt the error says "init needs two arguments". It would have been more helpful to include the actual error message...)