Using Global Variables Between Files

Using global variables between files?

The problem is you defined myList from main.py, but subfile.py needs to use it. Here is a clean way to solve this problem: move all globals to a file, I call this file settings.py. This file is responsible for defining globals and initializing them:

# settings.py

def init():
global myList
myList = []

Next, your subfile can import globals:

# subfile.py

import settings

def stuff():
settings.myList.append('hey')

Note that subfile does not call init()— that task belongs to main.py:

# main.py

import settings
import subfile

settings.init() # Call only once
subfile.stuff() # Do stuff with global var
print settings.myList[0] # Check the result

This way, you achieve your objective while avoid initializing global variables more than once.

How to share global variables between files in Python like C

You can do something like this to get varaible x in main.py , if you declare x as global inside foo() that mean you will be accessing the global x not local x.

#foo.py
x =10
def foo():
x = 8
print(" in foo x= " ,x)


#main.py
from conf import *
from foo import x, foo

if __name__ == "__main__":
print(" x imported from foo.py = ", x)
foo()

OUTPUT

x imported from foo.py  =  10
in foo x= 8

How do I share a global variable between c files?

file 1:

int x = 50;

file 2:

extern int x;

printf("%d", x);

How to share a global variable between files using header file?

Declaring an object as extern can be done many times. All it means is "this object's linkage data is somewhere else".

Your code doesn't contain the "somewhere else" definition.

file.h:

extern int i; // i is defined somewhere else

file.cpp:

int i = 0; // this is the definition

some-other-file.cpp:

extern int i; // will use the same i in file.cpp
// alternatively, to avoid duplicating the extern declaration,
// you can just include file.h

To summarize, in one of your cpp files, you need to add:

ib o[10];

Global variables between modules

when "*importing" something, it executes the code, and copy's the globals in to your globals. but if globals get alterd later, it won't re-copy the globals. the solution is, to re-import the file after test is called

file2.py:

def test():
global justTry
justTry = "hello"

main.py:

from file2 import *

def main():
print(justTry)

if __name__ == '__main__':
test()
from file2 import *
main()

Global Variable from a different file Python

Importing file2 in file1.py makes the global (i.e., module level) names bound in file2 available to following code in file1 -- the only such name is SomeClass. It does not do the reverse: names defined in file1 are not made available to code in file2 when file1 imports file2. This would be the case even if you imported the right way (import file2, as @nate correctly recommends) rather than in the horrible, horrible way you're doing it (if everybody under the Sun forgot the very existence of the construct from ... import *, life would be so much better for everybody).

Apparently you want to make global names defined in file1 available to code in file2 and vice versa. This is known as a "cyclical dependency" and is a terrible idea (in Python, or anywhere else for that matter).

So, rather than showing you the incredibly fragile, often unmaintainable hacks to achieve (some semblance of) a cyclical dependency in Python, I'd much rather discuss the many excellent way in which you can avoid such terrible structure.

For example, you could put global names that need to be available to both modules in a third module (e.g. file3.py, to continue your naming streak;-) and import that third module into each of the other two (import file3 in both file1 and file2, and then use file3.foo etc, that is, qualified names, for the purpose of accessing or setting those global names from either or both of the other modules, not barenames).

Of course, more and more specific help could be offered if you clarified (by editing your Q) exactly why you think you need a cyclical dependency (just one easy prediction: no matter what makes you think you need a cyclical dependency, you're wrong;-).

How do I share a global variable between multiple files?

If you truly want a global variable (not advisable, of course) then you're always 100% free to do

window.globalVar = 0;

in any of your modules.


The more robust solution would of course be to have this global variable sit in some sort of dedicated module, like

globalVar.js

export default {
value: 0
};

and then you could

import globalVal from './globalVar';

globalVal.value = 'whatever';

from any modules needed.

The only risk would be that webpack might duplicate this same "global" value into multiple bundles if you're code splitting, depending on your setup. So separate module would be using their own local copy of this not-so-global variable. EDIT - this isn't true. webpack never did this; that comment was based on a misunderstanding on my part.



Related Topics



Leave a reply



Submit