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
62
votes
5 answers
__main__ and scoping in python
I was somehow surprised by the following behavior:
def main():
print "%s" % foo
if __name__ == "__main__":
foo = "bar"
main()
i.e. a module function has access to enclosing variables in the __main__. What's the explanation for it?

David Cournapeau
- 78,318
- 8
- 63
- 70
62
votes
3 answers
Is it always safe to modify the `**kwargs` dictionary?
Using the Python function syntax def f(**kwargs), in the function a keyword argument dictionary kwargs is created, and dictionaries are mutable, so the question is, if I modify the kwargs dictionary, is it possible that I might have some effect…

Paul
- 10,381
- 13
- 48
- 86
62
votes
3 answers
Differences between declare, typeset and local variable in Bash
When typing variables in Bash, what is the difference between declare and typeset? When used inside a function: what is the difference between declare and typeset and local?
The only difference I have come across is that typeset is portable to ksh…

lecodesportif
- 10,737
- 9
- 38
- 58
62
votes
8 answers
How do I create a static local variable in Java?
I've read Java does not support static local variables unlike C/C++. Now if I want to code a function with a local variable, whose value should persist between function calls, how do I do that?
Should I resort to using instance variables?

gameover
- 11,813
- 16
- 59
- 70
61
votes
4 answers
Class static variable initialization order
I have a class A which has two static variables. I'd like to initialize one with another, unrelated static variable, just like this:
#include
class A
{
public:
static int a;
static int b;
};
int A::a = 200;
int a = 100;
int A::b…

QuantumPlus
- 473
- 3
- 6
61
votes
5 answers
Why does a Try/Catch block create new variable scope?
For example:
try
{
SomeObject someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(Exception e)
{
}
someObject.anotherMethod(); //can't access someObject!
But you can declare it before the try/catch block and then it works…

telkins
- 10,440
- 8
- 52
- 79
60
votes
3 answers
I can't reach any class member from a nested class in Kotlin
I want to access a member of the MainFragment class from PersonAdapter class but none of them are available. I tried making both the classes and the members public and private also but so far nothing worked.
I guess I'm missing something obvious…

ftibi93
- 707
- 1
- 5
- 8
60
votes
9 answers
How do I create a list of lambdas (in a list comprehension/for loop)?
I want to create a list of lambda objects from a list of constants in Python; for instance:
listOfNumbers = [1,2,3,4,5]
square = lambda x: x * x
listOfLambdas = [lambda: square(i) for i in listOfNumbers]
This will create a list of lambda objects,…

Smashery
- 57,848
- 30
- 97
- 128
60
votes
5 answers
Please explain the use of JavaScript closures in loops
I have read a number of explanations about closures and closures inside loops. I have a hard time understanding the concept. I have this code: Is there a way to reduce the code as much as possible so the concept of closure can be made clearer. I am…

CMS scripting
- 669
- 1
- 7
- 13
60
votes
8 answers
How do I pass variables across functions?
I want to pass values (as variables) between different functions.
For example, I assign values to a list in one function, then I want to use that list in another function:
list = []
def defineAList():
list = ['1','2','3']
print "For…

user2113818
- 829
- 1
- 9
- 12
59
votes
11 answers
Why is my HelloWorld function not declared in this scope?
#include
using namespace std;
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
I am getting the following compilation error with g++:
l1.cpp: In function 'int…

MatthewSot
- 3,516
- 5
- 39
- 58
59
votes
5 answers
scope of using declaration within a namespace
Is it safe (and correct) in a C++ header file to use the using declaration within a namespace as follows:
#include
namespace MyNamespace {
using boost::numeric::ublas::vector;
vector MyFunc(vector…

Brett Ryland
- 1,045
- 1
- 9
- 17
59
votes
5 answers
functions inside or outside jquery document ready
Up until now I just put all my jQuery goodness inside the $(document).ready() function, including simple functions used in certain user interactions.
But functions that don´t require the DOM document to be loaded or are only called afterwards…

Hans
- 856
- 2
- 8
- 10
59
votes
4 answers
Is there a generic way to memoize in Scala?
I wanted to memoize this:
def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out
So I wrote this and this surprisingly compiles and works (I am surprised because fib references itself in its declaration):
case class…

pathikrit
- 32,469
- 37
- 142
- 221
58
votes
3 answers
Static fields in a base class and derived classes
In an abstract base class if we have some static fields then what happens to them ?
Is their scope the classes which inherit from this base class or just the type from which it is inheriting (each subclass has it's own copy of the static field from…

Xaqron
- 29,931
- 42
- 140
- 205