Suppose I have two Python files:
abc.py
:from .config import *
update_a()
print_a() # prints 5
print(a) # prints 2 rather than 5 even after calling update_a() and using global in update_a()
config.py
:a = 2
def update_a():
global a
a = 5
def print_a():
global a
print(a) # prints 5
The global variable in config.py
does not seem to have the updated value when accessed from abc.py
.
If it's been created globally, then you'll be updating the global variable. You can override this behaviour by declaring it locally using var , but if you don't use var , then a variable name used in a function will be global if that variable has been declared globally.
On the Global Variable List panel, you can delete a variable by positioning your cursor on the line you want to delete and pressing the Delete key.
You don't. There are no global variables in Java.
The C language allows the redeclaration of the global variable. It means that this variable can get declared again when the first declaration doesn't lead to the initialization of the variable. It is possible because the second program works pretty well in the C language even if the first one fails during compilation.
When you do an import say from .config import *
, the variable a
is imported as a local scope. Any modification to a
will happen within the scope in abc.py
NOT in config.py
whereas the call to update_a()
and print_a()
is modifying the variable a
within config.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With