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
40
votes
4 answers

Python global keyword vs. Pylint W0603

Pylint W0603 states: Using the global statement. Used when you use the "global" statement to update a global variable. PyLint just try to discourage this usage. That doesn't mean you can not use it ! I wonder why is it so? Is there any more…
Jovik
  • 4,046
  • 6
  • 24
  • 24
39
votes
6 answers

How does one clear or remove a global in julia?

Is there any syntax that does something similar to MATLAB's "clear" i.e. if I have a global variable "a". How do I get rid of it? How do I do the analog of clear a
Mateo
  • 1,494
  • 1
  • 18
  • 27
39
votes
4 answers

Why can't I set a global variable in Python?

How do global variables work in Python? I know global variables are evil, I'm just experimenting. This does not work in python: G = None def foo(): if G is None: G = 1 foo() I get an error: UnboundLocalError: local variable 'G'…
slim
39
votes
5 answers

Insert variable into global namespace from within a function?

Is it possible to add an object to the global namespace, for example, by using globals() or dir()? def insert_into_global_namespace(var_name, value): globals()[var_name] = value insert_into_global_namespace('my_obj', 'an…
Dan
  • 763
  • 3
  • 8
  • 13
38
votes
6 answers

How can I access a shadowed global variable in C?

How can I access a shadowed global variable in C? In C++ I can use :: for the global namespace.
vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
38
votes
9 answers

Using globalThis in Typescript

I am trying to use globalThis in TypeScript and I would like some suggestions on how to write it in a better way. Current implementation is like this: Create a file types/global.d.ts and add inside interface Global { foo: string } declare let…
Nickey
  • 1,191
  • 1
  • 13
  • 21
38
votes
5 answers

Get variables from the outside, inside a function in PHP

I'm trying to figure out how I can use a variable that has been set outside a function, inside a function. Is there any way of doing this? I've tried to set the variable to "global" but it doesn't seems to work out as expected. A simple example of…
Henrikz
  • 431
  • 2
  • 5
  • 7
38
votes
4 answers

How to get global access to enum types in C#?

This is probably a stupid question, but I can't seem to do it. I want to set up some enums in one class like this: public enum Direction { north, east, south, west }; Then have that enum type accessible to all classes so that some other class…
lala
  • 2,052
  • 6
  • 24
  • 33
38
votes
2 answers

Elegant way to check if a global plugin variable is set in a vim plugin

I used to use vim during the last years for editing configs and scripts on remote servers. A couple of weeks ago I decided to take the next step and try to use (Mac)vim as my regular editor besides Sublime Text 2. Now I reached a point where I would…
Saucier
  • 4,200
  • 1
  • 25
  • 46
37
votes
13 answers

Why are global variables bad, in a single threaded, non-os, embedded application

Most of the objections I see to using global variables make sense since they refer to issues of multiple threads, thread safety, etc. But in a small, single threaded, non-OS, case, what objections do you have? In my case, I'm writing my embedded…
loneRanger
  • 812
  • 1
  • 8
  • 14
37
votes
3 answers

Declaring a global variable in MATLAB

Is there a way to declare global variables in MATLAB? Please don't respond with: global x y z; Because I can also read the help files. I've declared a global variable, x, and then done something like this: function[x] = test() global x; …
Amit
  • 7,688
  • 17
  • 53
  • 68
37
votes
5 answers

Python: Sharing global variables between modules and classes therein

I know that it's possible to share a global variable across modules in Python. However, I would like to know the extent to which this is possible and why. For example, global_mod.py x = None mid_access_mod.py from global_mod import * class…
emish
  • 2,813
  • 5
  • 28
  • 34
37
votes
3 answers

Swift globals and global functions in objective c

the documentation says: Global constants defined in C and Objective-C source files are automatically imported by the Swift compiler as Swift global constants. But it doesn't say anything about the other way around. I need to define a global…
Ali
  • 18,665
  • 21
  • 103
  • 138
37
votes
8 answers

Why can't I create an array with size determined by a global variable?

Why does the array a not get initialized by global variable size? #include int size = 5; int main() { int a[size] = {1, 2, 3, 4, 5}; printf("%d", a[0]); return 0; } The compilation error is shown as variable-sized object…
Ashish Yadav
  • 1,667
  • 6
  • 20
  • 26
37
votes
4 answers

How can I define an AngularJS service using a TypeScript class that doesn't pollute the global scope?

I am using AngularJS and TypeScript. I want to implement an AngularJS service using a Typescript class, like this: class HelloService { public getWelcomeMessage():String { return "Hello"; …