Questions tagged [global-variables]

Global variables are variables that are accessible from all elements of a software component.

Global variables are variables that are accessible from all elements of a software component.

Global variables are used to share information between elements of a component and to store a part of the component's state. They therfore:

  • create a hidden coupling between all the elements that use them;
  • are a source of accidental bugs and inconsistencies, when changes and side-effects are not sufficiently controlled.
8810 questions
3897
votes
25 answers

Using global variables 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 often causes UnboundLocalError. The precise…
user46646
  • 153,461
  • 44
  • 78
  • 84
1226
votes
19 answers

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 files, but how does that work precisely? Where do I…
shilpa
677
votes
12 answers

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 to see its value or that it was even…
Guss
  • 30,470
  • 17
  • 104
  • 128
608
votes
17 answers

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 savedInstanceState) { …
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
446
votes
6 answers

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
using its ID as a variable name or as a…
KooiInc
  • 119,216
  • 31
  • 141
  • 177
423
votes
3 answers

Printing all global variables/local variables?

How can I print all global variables/local variables? Is that possible in gdb?
cpuer
  • 7,413
  • 14
  • 35
  • 39
368
votes
6 answers

Python function global variables?

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 global copy of a variable created in a separate…
Akshat Shekhar
  • 3,711
  • 2
  • 14
  • 4
358
votes
12 answers

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 navigation bar and restricts access to parts of the…
Lightbulb1
  • 12,882
  • 6
  • 22
  • 23
346
votes
11 answers

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 even without global: >>> sub = ['0', '0', '0',…
nik
  • 8,387
  • 13
  • 36
  • 44
326
votes
14 answers

Android global variable

How can I create global variable keep remain values around the life cycle of the application regardless which activity running.
d-man
  • 57,473
  • 85
  • 212
  • 296
307
votes
28 answers

What are the pros and cons in use of global variables?

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 objective advantages and drawbacks that arise when using…
timp
288
votes
14 answers

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' referenced before assignment in newer versions…
tba
  • 6,229
  • 8
  • 43
  • 63
216
votes
8 answers

Why does this UnboundLocalError occur (closure)?

What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
Randomblue
  • 112,777
  • 145
  • 353
  • 547
206
votes
16 answers

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 tried changing the validate_password_length variable…
Alex Ryan
  • 3,719
  • 5
  • 25
  • 41
206
votes
8 answers

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 them. In order to learn Rust, I'm currently…
Brian Oh
  • 9,604
  • 12
  • 49
  • 68
1
2 3
99 100