0

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)

Drdilyor
  • 1,250
  • 1
  • 12
  • 30
  • You could create your own God-object that you assign to. Just create a class (or use a dictionary), place that in a file, then import the file and use that object as needed. [`SimpleNamespace`](https://docs.python.org/3/library/types.html?highlight=namespace#types.SimpleNamespace) may be a good tool to try. – Carcigenicate Jun 07 '21 at 15:45
  • What do you mean by accessible in "other files" ? – Tom Lee Jun 07 '21 at 16:32
  • @TomLee https://codesandbox.io/s/quirky-mayer-gsbwf?file=/src/index.js – Drdilyor Jun 07 '21 at 16:39
  • Don't think there is something like that for python, but you can consider sending the variable value from one file to another when needing to process the data. – Tom Lee Jun 07 '21 at 16:42

1 Answers1

0

You can assign them to __builtins__:

main.py

__builtins__.my_var = 'Hello!'
import other

other.py

print(my_var)

Outputs: Hello!

Drdilyor
  • 1,250
  • 1
  • 12
  • 30