How to change a module variable from another module?
You are using from bar import a
. a
becomes a symbol in the global scope of the importing module (or whatever scope the import statement occurs in).
When you assign a new value to a
, you are just changing which value a
points too, not the actual value. Try to import bar.py
directly with import bar
in __init__.py
and conduct your experiment there by setting bar.a = 1
. This way, you will actually be modifying bar.__dict__['a']
which is the 'real' value of a
in this context.
It's a little convoluted with three layers but bar.a = 1
changes the value of a
in the module called bar
that is actually derived from __init__.py
. It does not change the value of a
that foobar
sees because foobar
lives in the actual file bar.py
. You could set bar.bar.a
if you wanted to change that.
This is one of the dangers of using the from foo import bar
form of the import
statement: it splits bar
into two symbols, one visible globally from within foo
which starts off pointing to the original value and a different symbol visible in the scope where the import
statement is executed. Changing a where a symbol points doesn't change the value that it pointed too.
This sort of stuff is a killer when trying to reload
a module from the interactive interpreter.
How to change variable value from another module in javascript
foo
is a primitive. You can't change it's the value assigned to it in the other module when it's a primitive, you can only change the value foo
has been assigned in scope of the current module.
You could always do it with functions however:
let foo = 'initial value';
export const getFoo = () => foo;
export const setFoo = (val) => (foo = val);
import { getFoo, setFoo } from 'someModule';
getFoo(); // initial value;
// call this when necessary
setFoo('jim');
import { getFoo } from 'someModule';
// when called after `setFoo` has been called in the other module
getFoo(); // 'jim'
Python Changing module variables in another module
You should be able to do:
import foo
foo.arbit = 'new value'
Python: change global variable from within another file
All that from config import ADDRESS, change_address
does is take ADDRESS
and change_address
from your config
module's namespace and dumps it into your current module's name-space. Now, if you reassign the value of ADDRESS
in config
's namespace, it won't be seen by the current module - that's how name-spaces work. It is like doing the following:
>>> some_namespace = {'a':1}
>>> globals().update(some_namespace)
>>> a
1
>>> some_namespace
{'a': 1}
>>> some_namespace['a'] = 99
>>> a
1
>>> some_namespace
{'a': 99}
The simplest solution? Don't clobber name-spaces:
import config
config.change_address("192.168.10.100")
print("new address " + config.ADDRESS)
Manipulate module variables from another module
When you create a separate process, the current global state is copied to the new process. Any changes you make afterward (like changing global variables) aren't going to be shared with the other process.
The multiprocessing
package has tools to help share state between processes
In this case, you probably want to use a Manager
to share these settings between the processes, rather than global variables.
from multiprocessing import Manager, Process
manager = Manager()
state = manager.dict()
state['pause'] = False
state['skip'] = False
state['quit'] = False
youtube_process = Process(target = youtube.Youtube, args=(query, state))
# Process will get this change.
state['quit'] = True
How to change the value of a module variable from within an object of another module?
Staring at your code for a bit, I think i know what might be throwing you off. The python script invoked as python foo.py
will end up being a module (in sys.modules
) called __main__
. That means the bottom bit of your mainmodule.py
shows is loaded and compiled and run once, with __name__ == "__main__"
, which causes some things to happen. That module imports submodule
, which has not been imported yet, so that gets loaded and run.
submodule
in turn tries to import mainmodule
. Although that file has been executed before, it's not known to the interpreter by that module name, so mainmodule.py
gets run again, this time with __name__ == "mainmodule"
(which isn't the same as "__main__"
, so the if
suite at the bottom is skipped).
That means you have two copies of container
, one in the module who's name is __main__
, and one in a module named mainmodule
. The fact that both are from a file called ./mainmodule.py
is not relevant.
There are a few ways to fix this. One is to always import the real immediately, as in:
# mainmodule.py
import submodule
class Foo:
pass
if __name__ == "__main__":
import mainmodule
mainmodule.Foo()
Another option is to move the code inside the if
to another file.
change the contents of a global variable from another module in Julia
A module can only change its own global variables, not those of other modules. There is a way, of course, to force a module to change its own global variables — by running toplevel code through eval
.
julia> module A
x = 1
y = 2
end
A
julia> module B
using ..A
println("A.x = ", A.x) # can access
A.y = 3 # can't set
end
A.x = 1
ERROR: cannot assign variables in other modules
julia> A.y
2
julia> module C
using ..A
println("A.x = ", A.x) # can access
@eval(A, y = 3) # set y = 3, FROM module A
println("A.y = ", A.y) # value of y updated
end
A.x = 1
A.y = 3
C
julia> A.y
3
However, mutating global state — especially some other module's global state — is a code smell and should be avoided if at all possible.
ES6 - How to modify a variable in other modules
You cannot assign new values to exported variables, only the module itself can do that (and when it does, it can be pretty confusing, so I'd recommend to avoid this).
You can mutate exported objects though, e.g. G.translog.push(…)
or G.translog.length = 0
.
How to make a cross-module variable?
I don't endorse this solution in any way, shape or form. But if you add a variable to the __builtin__
module, it will be accessible as if a global from any other module that includes __builtin__
-- which is all of them, by default.
a.py contains
print foo
b.py contains
import __builtin__
__builtin__.foo = 1
import a
The result is that "1" is printed.
Edit: The __builtin__
module is available as the local symbol __builtins__
-- that's the reason for the discrepancy between two of these answers. Also note that __builtin__
has been renamed to builtins
in python3.
Related Topics
Using Multipartposthandler to Post Form-Data with Python
Add Sum of Values of Two Lists into New List
Numpy - Create Matrix with Rows of Vector
E731 Do Not Assign a Lambda Expression, Use a Def
Pil Thumbnail Is Rotating My Image
How to Use Raw Socket in Python
How to Select All Columns Whose Names Start with X in a Pandas Dataframe
Load CSV Data into MySQL in Python
Two Values from One Input in Python
How to Use Pip to Install a Package from a Private Github Repository
Explicitly Select Items from a List or Tuple
How to Use Inspect to Get the Caller's Info from Callee in Python
How to Convert List of Key-Value Tuples into Dictionary
Executing Command Using Paramiko Exec_Command on Device Is Not Working
No Module Named 'Polls.Apps.Pollsconfigdjango'; Django Project Tutorial 2