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.

16290 questions
317
votes
8 answers

What's the scope of a variable initialized in an if statement?

I'm new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == '__main__': x = 1 print x In other languages I've worked in, this code would throw an…
froadie
  • 71,770
  • 69
  • 154
  • 228
271
votes
10 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,476
  • 9
  • 44
  • 73
263
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
  • 63,773
  • 66
  • 242
  • 344
257
votes
6 answers

Underscore prefix for property and method names in JavaScript

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,076
  • 6
  • 42
  • 62
244
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
  • 9,280
  • 21
  • 55
  • 86
230
votes
12 answers

UnboundLocalError on local variable when reassigned after first use

The following code works as expected in both Python 2.5 and 3.0: a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) print(b) print(c) # (A) #c+=1 # (B) test() However, when I uncomment line (B), I get an…
tba
  • 5,411
  • 6
  • 38
  • 61
229
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,350
  • 3
  • 20
  • 24
224
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
  • 108,984
  • 95
  • 246
  • 308
217
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
  • 2,573
  • 4
  • 15
  • 13
207
votes
9 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,270
  • 7
  • 24
  • 38
202
votes
6 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
  • 17,532
  • 20
  • 57
  • 59
200
votes
3 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,186
  • 3
  • 32
  • 55
196
votes
5 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
  • 41,133
  • 5
  • 39
  • 31
191
votes
12 answers

What is the difference between 'my' and 'our' in Perl?

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
188
votes
8 answers

Don't understand why UnboundLocalError occurs (closure)

What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
Randomblue
  • 98,379
  • 133
  • 328
  • 526