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
200
votes
10 answers

How to declare a global variable in php?

I have code something like this:
LIGHT
  • 5,604
  • 10
  • 35
  • 78
183
votes
4 answers

Can I use __init__.py to define global variables?

I want to define a constant that should be available in all of the submodules of a package. I've thought that the best place would be in in the __init__.py file of the root package. But I don't know how to do this. Suppose I have a few subpackages…
Andrei Vajna II
  • 4,642
  • 5
  • 35
  • 38
181
votes
3 answers

What happens to global and static variables in a shared library when it is dynamically linked?

I'm trying to understand what happens when modules with globals and static variables are dynamically linked to an application. By modules, I mean each project in a solution (I work a lot with visual studio!). These modules are either built into…
Raja
  • 2,846
  • 5
  • 19
  • 28
181
votes
3 answers

Global variables in R

I am poking into the manuals, I wanted to ask the community: How can we set global variables inside a function?
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
180
votes
4 answers

Why are global variables evil?

I'm trying to find out why the use of global is considered to be bad practice in python (and in programming in general). Can somebody explain? Links with more info would also be appreciated.
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
176
votes
8 answers

Share variables between files in Node.js?

Here are 2 files: // main.js require('./module'); console.log(name); // prints "foobar" // module.js name = "foobar"; When I don't have "var" it works. But when I have: // module.js var name = "foobar"; name will be undefined in main.js. I have…
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
167
votes
4 answers

How do I call setattr() on the current module?

What do I pass as the first parameter "object" to the function setattr(object, name, value), to set variables on the current module? For example: setattr(object, "SOME_CONSTANT", 42); giving the same effect as: SOME_CONSTANT = 42 within the module…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
158
votes
8 answers

Most Pythonic way to provide global configuration variables in config.py?

In my endless quest in over-complicating simple stuff, I am researching the most 'Pythonic' way to provide global configuration variables inside the typical 'config.py' found in Python egg packages. The traditional way (aah, good ol' #define!) is…
Rigel Di Scala
  • 3,150
  • 2
  • 17
  • 23
158
votes
10 answers

How to modify a global variable within a function in bash?

I'm working with this: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) I have a script like below: #!/bin/bash e=2 function test1() { e=4 echo "hello" } test1 echo "$e" Which returns: hello 4 But if I assign the result of the…
mllamazares
  • 7,876
  • 17
  • 61
  • 89
156
votes
9 answers

How do I declare a global variable in VBA?

I wrote the following code: Function find_results_idle() Public iRaw As Integer Public iColumn As Integer iRaw = 1 iColumn = 1 And I get the error message: "invalid attribute in Sub or Function" Do you know what I did wrong? I…
Nimrod
  • 2,343
  • 3
  • 17
  • 7
155
votes
9 answers

Global variables in Javascript across multiple files

A bunch of my JavaScript code is in an external file called helpers.js. Inside the HTML that calls this JavaScript code I find myself in need of knowing if a certain function from helpers.js has been called. I have attempted to create a global…
Goro
  • 9,919
  • 22
  • 74
  • 108
155
votes
1 answer

Modify a globally scoped variable inside of an anonymous function

I was playing around with anonymous functions in PHP and realized that they don't seem to reach variables outside of them. Is there any way to get around this problem? Example: $variable = "nothing"; functionName($someArgument, function() { …
einord
  • 2,278
  • 2
  • 20
  • 26
151
votes
4 answers

How to create a global variable?

I have a global variable that needs to be shared among my ViewControllers. In Objective-C, I can define a static variable, but I can't find a way to define a global variable in Swift. Do you know of a way to do it?
czzhengkw
  • 1,521
  • 2
  • 10
  • 5
144
votes
6 answers

How to declare a global variable in JavaScript

How can I declare a global variable in JavaScript?
Dancer
  • 17,035
  • 38
  • 129
  • 206
133
votes
7 answers

Global Variables in Dart

I try to create a Dart single page application. I have created a first custom element (custom-application) which contains the whole application. It has a container in it which is used to render views. And a side nav which will contain user…
T00rk
  • 2,178
  • 3
  • 17
  • 23