Search code examples
pythonglobal-variables

Is there a javascript's window like object in python?


I mean in javascript it is possible to create a variable accessible by everywhere in everyfile by assigning property to window object:

window.myVar = 'hello'

And use it like:

console.log(myVar)

browser 'window' object equivalent in Python? [closed] doesn't answer my question because assigning to globals() doesn't make it accessible from other files.

(I know global variables are bad)


Solution

  • You can assign them to __builtins__:

    main.py

    __builtins__.my_var = 'Hello!'
    import other
    

    other.py

    print(my_var)
    

    Outputs: Hello!