Suppose I have the following in a python file with the name: file1.py
a=1 def f(x): return x+a Now, in another file (Let's say file2.py) if I import the above function, that is: from file1 import f
and then, evaluate it at a certain value, say; print(f(2)) gives the value 3 because, 2+a=2+1=3. That means the code has used the value of a defined in file1.py
Also even if I have the file2 as,
from file1 import f a=10 print(f(2)) It still gives the same answer as before. Which means the function has used the value of a defined in file1
So my question is, whether there is a way to write the code in file2 so that it uses the a defined there.
I know that I can define the function as
def f(x,a): return x+a But for my case it would be really helpful, if I can leave the function only with one variable....
Appreciate your help
https://stackoverflow.com/questions/65964122/calling-a-function-in-a-separate-file-with-parameters-defined-on-the-initial-one January 30, 2021 at 10:41AM
没有评论:
发表评论