8,880 questions
4042 votes
27 answers
4.3m views
How can I use a global variable in a function?
How do I create or use a global variable inside a function? How do I use a global variable that was defined in one function inside other functions? Failing to use the global keyword where appropriate ...
1280 votes
19 answers
1.0m views
How do I use extern to share variables between source files?
I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source ...
697 votes
13 answers
926k views
How can I unset a JavaScript variable?
I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script, but I don't want another script that will run later ...
608 votes
17 answers
313k views
How to declare global variables in Android?
I am creating an application which requires login. I created the main and the login activity. In the main activity onCreate method I added the following condition: public void onCreate(Bundle ...
457 votes
6 answers
63k views
Do DOM tree elements with IDs become global properties?
Working on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome: For a given HTMLElement with an id in the DOM tree, it is possible to retrieve the &...
456 votes
4 answers
479k views
Printing all global variables/local variables?
How can I print all global variables/local variables? Is that possible in gdb?
377 votes
6 answers
885k views
Python function global variables? [duplicate]
I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (I am trying to call the ...
360 votes
11 answers
453k views
Why isn't the 'global' keyword needed to access a global variable?
From my understanding, Python has a separate namespace for functions, so if I want to use a global variable in a function, I should probably use global. However, I was able to access a global variable ...
358 votes
12 answers
476k views
Global variables in AngularJS
I have a problem where i'm initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to control things such as the ...
329 votes
14 answers
447k views
Android global variable
How can I create global variable keep remain values around the life cycle of the application regardless which activity running.
317 votes
15 answers
120k views
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
When I try this code: a, b, c = (1, 2, 3) def test(): print(a) print(b) print(c) c += 1 test() I get an error from the print(c) line that says: UnboundLocalError: local variable 'c' ...
313 votes
28 answers
188k views
What are the pros and cons in use of global variables? [closed]
In C/C++, there is a widespread opinion that global variables are "bad" and should be avoided when possible. Leaving aside the question whether this opinion is correct or not, what are the ...
235 votes
10 answers
251k views
Is it possible to use global variables in Rust?
I know that in general, global-variables are to be avoided. Nevertheless, I think in a practical sense, it is sometimes desirable (in situations where the variable is integral to the program) to use ...
230 votes
8 answers
333k views
Why does this UnboundLocalError occur (closure)? [duplicate]
What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
212 votes
17 answers
428k views
How do I turn off the mysql password validation?
It seems that I may have inadvertently loaded the password validation plugin in MySQL 5.7. This plugin seems to force all passwords to comply to certain rules. I would like to turn this off. I've ...