0

I call a method (which is imported from a separate file) multiple times. This method contains a variable whose value I would like to retain between all the times I call the method (without returning it and having it as an argument). For example, I would like to compute a mean of all the values computed across different calls to that function. Is it possible (for example, by making it global or in any other way)?

In file1.py I make the following import

from file2 import my_method

my_method()

file2.py contains my_method

def my_method():
    #some computation involving variable `a`
    return  # a is not returned

We would like to retain a across different calls. The only thing that comes to my mind now is to save the variable a and read it back every time I call the method.

Note: I can edit file1.py and file2.py but I cannot modify the arguments nor the return values of my_method

cerebrou
  • 5,353
  • 15
  • 48
  • 80
  • 3
    If you want to carry state between function calls, you need to use a global variable or make the function a method of a class whose instances store the state. You'll need to be more precise about what you are trying to do (preferably by providing a minimal code example we could run). – chepner Jan 08 '21 at 20:40
  • Does this answer your question? [Passing an integer by reference in Python](https://stackoverflow.com/questions/15148496/passing-an-integer-by-reference-in-python) – Gigioz Jan 08 '21 at 20:40
  • @chepner those arent the only ways - there are also function and method attributes. However if by ‘method’ you mean this of a class, then use an instance attribute of course. But for standalone function then yes function attributes would be a reasonable way to do this. See https://stackoverflow.com/questions/338101/python-function-attributes-uses-and-abuses also see https://www.python.org/dev/peps/pep-0232/ – DisappointedByUnaccountableMod Jan 08 '21 at 20:44
  • I don't consider either to be a good idea. – chepner Jan 08 '21 at 20:45
  • 1
    Globals are definitely not a good idea. Instance attributes certainly seem the most sensible route. – DisappointedByUnaccountableMod Jan 08 '21 at 20:46
  • @chepner A global is almost certainly not the correct solution here. Perhaps creating a class with a field is, but that should be driven by the problem domain, not trying to find the solution domain. Another alternative is to pass the value in as a parameter. But again, whether or not this is a good solution is determined by the problem being solved. – Code-Apprentice Jan 08 '21 at 20:48

1 Answers1

2

It sounds like you need to revisit the design of your functions. When you have a local variable's value that needs to be retained, then that variable shouldn't be local. Instead, it should be passed into the function as a parameter or possibly stored as a class field.

There are many different ways to solve your problem, but it all comes down to design. We will need more details to give specific suggestions.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268