Questions tagged [scope]

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.

17524 questions
381
votes
10 answers

What underlies this JavaScript idiom: var self = this?

I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo: function Note() { var self = this; var note = document.createElement('div'); note.className = 'note'; note.addEventListener('mousedown', function(e) { return…
Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
370
votes
5 answers

Python 3: UnboundLocalError: local variable referenced before assignment

The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment: Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("Result 1") elif Var2 == 1 and Var1 > 0: …
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
316
votes
8 answers

Is it possible to declare two variables of different types in a for loop?

Is it possible to declare two variables of different types in the initialization body of a for loop in C++? For example: for(int i=0,j=0 ... defines two integers. Can I define an int and a char in the initialization body? How would this be done?
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
312
votes
12 answers

Why use a public method in an internal class?

There is a lot of code in one of our projects that looks like this: internal static class Extensions { public static string AddFoo(this string s) { if (s == null) { return "Foo"; } return…
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
289
votes
6 answers

Is the underscore prefix for property and method names merely a convention?

Is the underscore prefix in JavaScript only a convention, like for example in Python private class methods are? From the 2.7 Python documentation: “Private” instance variables that cannot be accessed except from inside an object don’t exist in…
Kenny Meyer
  • 7,849
  • 6
  • 45
  • 66
289
votes
5 answers

How to create module-wide variables in Python?

Is there a way to set up a global variable inside of a module? When I tried to do it the most obvious way as appears below, the Python interpreter said the variable __DBNAME__ did not exist. ... __DBNAME__ = None def initDB(name): if not…
daveslab
  • 10,000
  • 21
  • 60
  • 86
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
275
votes
8 answers

A variable modified inside a while loop is not remembered

In the following program, if I set the variable $foo to the value 1 inside the first if statement, it works in the sense that its value is remembered after the if statement. However, when I set the same variable to the value 2 inside an if which is…
Eric Lilja
  • 3,323
  • 4
  • 18
  • 15
258
votes
8 answers

Can I access variables from another file?

Is it possible to use a variable in a file called first.js inside another file called second.js? first.js contains a variable called colorcodes.
SAK
  • 3,780
  • 7
  • 27
  • 38
257
votes
8 answers

Scoping in Python 'for' loops

I'm not asking about Python's scoping rules; I understand generally how scoping works in Python for loops. My question is why the design decisions were made in this way. For example (no pun intended): for foo in xrange(10): bar = 2 print(foo,…
chimeracoder
  • 20,648
  • 21
  • 60
  • 60
254
votes
5 answers

How do I pass the this context to a function?

I thought this would be something I could easily google, but maybe I'm not asking the right question... How do I set whatever "this" refers to in a given javascript function? for example, like with most of jQuery's functions such as: …
user126715
  • 3,648
  • 3
  • 23
  • 25
242
votes
10 answers

Accessing class variables from a list comprehension in the class definition

How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3: class Foo: x = 5 y = [x for i in range(1)] Python 3.2 gives the error: NameError: global…
Mark Lodato
  • 50,015
  • 5
  • 41
  • 32
226
votes
9 answers

Is it possible to define more than one function per file in MATLAB, and access them from outside that file?

When I was studying for my undergraduate degree in EE, MATLAB required each function to be defined in its own file, even if it was a one-liner. I'm studying for a graduate degree now, and I have to write a project in MATLAB. Is this still a…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
225
votes
4 answers

Access a variable outside the scope of a Handlebars.js each loop

I have a handlebars.js template, just like this: {{externalValue}} And this is the generated…
lucke84
  • 4,516
  • 3
  • 37
  • 58
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