Scope is an enclosing context where values and expressions are associated. Use this tag for questions about different types of scope as well for questions where scope may be unclear.
Questions tagged [scope]
17524 questions
99
votes
6 answers
How can I access global variable inside class in Python
I have this:
g_c = 0
class TestClass():
global g_c
def run(self):
for i in range(10):
g_c = 1
print(g_c)
t = TestClass()
t.run()
print(g_c)
how can I actually modify my global variable g_c?

torayeff
- 9,296
- 19
- 69
- 103
98
votes
6 answers
Surprised that global variable has undefined value in JavaScript
Today, I got completely surprised when I saw that a global variable has undefined value in a certain case.
Example:
var value = 10;
function test() {
//A
console.log(value);
var value = 20;
//B
…

iamjustcoder
- 4,714
- 10
- 33
- 46
97
votes
2 answers
Access self from decorator
In setUp() method of unittest I've setup some self variables, which are later referenced in actual tests. I've also created a decorator to do some logging. Is there a way in which I can access those self variables from decorator?
For the sake of…

kevin
- 2,172
- 3
- 18
- 19
94
votes
7 answers
Preserving a reference to "this" in JavaScript prototype functions
I'm just getting into using prototypal JavaScript and I'm having trouble figuring out how to preserve a this reference to the main object from inside a prototype function when the scope changes. Let me illustrate what I mean (I'm using jQuery…

Jimmy
- 35,686
- 13
- 80
- 98
93
votes
6 answers
Why does assigning to my global variables not work in Python?
I'm having terrible trouble trying to understand python scoping rules.
With the following script:
a = 7
def printA():
print "Value of a is %d" % (a)
def setA(value):
a = value
print "Inside setA, a is now %d" %(a)
print "Before…

Free Wildebeest
- 7,272
- 8
- 38
- 42
93
votes
2 answers
Creating a class within a function and access a function defined in the containing function's scope
Edit:
See my full answer at the bottom of this question.
tl;dr answer: Python has statically nested scopes. The static
aspect can interact with the implicit variable declarations, yielding non-obvious results.
(This can be especially surprising…

Gabriel Grant
- 5,415
- 2
- 32
- 40
92
votes
3 answers
How to make a variable inside a try/except block public?
How can I make a variable inside the try/except block public?
import urllib.request
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError,…
user3348051
90
votes
5 answers
What's the difference between a global variable and a 'window.variable' in JavaScript?
I'm reading the Backbone.js documents and am seeing a lot of code that assigns attributes to the window object:
window.something = "whatever";
What's the difference between calling this code, and just assigning the variable and creating a global…

Derick Bailey
- 72,004
- 22
- 206
- 219
90
votes
6 answers
Non-declaration statement outside function body in Go
I'm building a Go library for an API that offers JSON or XML formatted data.
This API requires me to request a session_id every 15 minutes or so, and use that in calls. For…

sergserg
- 21,716
- 41
- 129
- 182
89
votes
11 answers
Declaring an object before initializing it in c++
Is it possible to declare a variable in c++ without instantiating it? I want to do something like this:
Animal a;
if( happyDay() )
a( "puppies" ); //constructor call
else
a( "toads" );
Basially, I just want to declare a outside of the…

Quantum7
- 3,165
- 3
- 34
- 45
88
votes
7 answers
shared global variables in C
How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at…

Claudiu
- 224,032
- 165
- 485
- 680
87
votes
3 answers
Angularjs, passing scope between routes
I have a situation with a form that stretches over several pages (might not be ideal, but that is how it is). I'd like to have one scope for the entire form that gets filled in as you go along, so that if the user goes back and forth between steps…

Erik Honn
- 7,576
- 5
- 33
- 42
86
votes
4 answers
In Haskell, when do we use in with let?
In the following code, the last phrase I can put an in in front. Will it change anything?
Another question: If I decide to put in in front of the last phrase, do I need to indent it?
I tried without indenting and hugs complains
Last generator in…

McBear Holden
- 5,741
- 7
- 33
- 55
86
votes
9 answers
Using json_encode on objects in PHP (regardless of scope)
I'm trying to output lists of objects as json and would like to know if there's a way to make objects usable to json_encode? The code I've got looks something like
$related = $user->getRelatedUsers();
echo json_encode($related);
Right now, I'm…

A. Rager
- 1,950
- 2
- 15
- 16
86
votes
5 answers
Passing a variable from one php include file to another: global vs. not
I'm trying to pass a variable from one include file to another. This is NOT working unless I declare the variable as global in the second include file. However, I do NOT need to declare it as global in the file that is calling the first include. For…

maxedison
- 17,243
- 14
- 67
- 114