Search code examples
pythonscopestaticpython-importvariable-assignment

Is there any way to make a static variable in Python?


I'm not exactly a noob but I'm also not the best Python dev. Recently I've been trying to make a program that creates a variable in the global scope of main.py, a function in main calls file2.foo(), and file2 imports main and edits the variable, and then main calls file3.bar(), where file3 imports that same variable and it would do things with the updated data from file2

I know that there's probably no real solution to this and that I should just use returns to update the var in main, but I was hoping that some of y'all might know of a way to make it static or have an easier solution for pass-by-reference

An example of what I'm trying to do:

main.py:

import file2, file3
var = 0

if __name__ == '__main__':
    var = 3
    file2.foo()
    # var should now be 6
    file3.bar()
    # var should now be 7
    print(var)

file2.py:

import main

def foo():
    main.var += 3

file3.py:

import main

def bar():
main.var += 1

I know that importing a file reinitializes it's variables like a class, but I also know that python classes can have static variables if all instances of the class are in the same file. I've tried having a 4th file just for storing var = 0 and having all three others import that, and I've also tried having a class in that 4th file set up like this: 4th file:

class a:
    static_var = 0
    def __init__(self) -> None:
        self.var = static_var

and then having all other files do from file4 import a; global = a() , then access it with global.var += some_integer

I've tried looking this up but as far as I can see no one else has this same problem. Any help is appreciated, especially if it's just to tell me there's no way to do it. Thanks a ton!


Solution

  • Not that I necessarily support this approach, but you can achieve what you want by putting your "static variable" in a new file:

    # main.py
    import file1, file2, file3                                                                              
                                                                                                            
                                                                                                            
    if __name__ == '__main__':
        print("in main:", file1.var)                                                                             
        file2.foo()                                                                                         
        # var should now be 6                                                                               
        file3.bar()                                                                                         
        # var should now be 7                                                                               
        print("in main:", file1.var)
    
    # file1.py
    var = 0
    
    #file2.py
    import file1                                                                                            
                                                                                                            
                                                                                                            
    def foo():                                                                                              
        file1.var = 6
    
    # file3.py
    import file1                                                                                            
                                                                                                            
                                                                                                            
    def bar():                                                                                              
        file1.var += 1
    

    You should benefit from reading how to import files in the Python documentation: https://docs.python.org/3/reference/import.html