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
51
votes
16 answers
How can a function access its own attributes?
is it possible to access the python function object attributes from within the function scope?
e.g. let's have
def f():
return SOMETHING
f._x = "foo"
f() # -> "foo"
now, what SOMETHING has to be, if we want to have the _x attribute…

mykhal
- 19,175
- 11
- 72
- 80
51
votes
6 answers
scala class constructor parameters
What's the difference between:
class Person(name: String, age: Int) {
def say = "My name is " + name + ", age " + age
}
and
class Person(val name: String, val age: Int) {
def say = "My name is " + name + ", age " + age
}
Can I declare…

zihaoyu
- 5,483
- 11
- 42
- 46
51
votes
2 answers
Global dictionaries don't need keyword global to modify them?
I wonder why I can change global dictionary without global keyword? Why it's mandatory for other types? Is there any logic behind this?
E.g. code:
#!/usr/bin/env python3
stringvar = "mod"
dictvar = {'key1': 1,
'key2': 2}
def foo():
…

Jovik
- 4,046
- 6
- 24
- 24
50
votes
9 answers
Which design pattern(s) take advantage of JavaScript's hoisting behavior?
Ben Cherry's excellent article explains hoisting in JavaScript adequately. My problem, however, is that I cannot conceive a use case for this notorious perpetrator of confusion. Please explain if there is a design pattern that actually takes…

David Rivers
- 2,896
- 1
- 31
- 39
50
votes
4 answers
Tensorflow: How to get a tensor by name?
I'm having trouble recovering a tensor by name, I don't even know if it's possible.
I have a function that creates my graph:
def create_structure(tf, x, input_size,dropout):
with tf.variable_scope("scale_1") as scope:
W_S1_conv1 =…

protas
- 617
- 1
- 5
- 10
50
votes
2 answers
Dagger2 Custom Scopes : How do custom-scopes (@ActivityScope) actually work?
I am reading the source code for Dagger2 Component Scopes Test on GitHub, and I've seen a "custom scope" defined for activities called @ActivityScope, but I've seen it in other projects including the 4-module CleanArchitecture that has its…

EpicPandaForce
- 79,669
- 27
- 256
- 428
50
votes
4 answers
Node.js variable declaration and scope
When I type this in node.js, I get undefined.
var testContext = 15;
function testFunction() {
console.log(this.testContext);
}
testFunction();
=>undefined
Without var keyword, it passes (=>15). It's working in the Chrome console (with and without…

IGRACH
- 3,506
- 6
- 33
- 48
49
votes
1 answer
How do the scoping rules work with classes?
Consider the following snippet of python code:
x = 1
class Foo:
x = 2
def foo():
x = 3
class Foo:
print(x) # prints 3
Foo.foo()
As expected, this prints 3.
But, if we add a single line to the above snippet, the…

snickerdoodles777
- 655
- 5
- 9
49
votes
5 answers
Function/variable scope (pass by value or reference?)
I'm completely confused by Lua's variable scoping and function argument passing (value or reference).
See the code below:
local a = 9 -- since it's define local, should not have func scope
local t = {4,6} -- since it's define local, should…

frooyo
- 1,863
- 3
- 19
- 21
48
votes
4 answers
C++ namespace alias in entire class scope
I expected to be able to use a namespace alias in a class declaration but get a compiler syntax error.
struct MyClass
{
namespace abc = a_big_namespace;
void fn() {
abc::test();
}
};
The only way I can get it to work is to put…

Ant
- 1,668
- 2
- 18
- 35
48
votes
6 answers
Variables set during $.getJSON function only accessible within function
This may be more of a scoping question. I'm trying to set a JSON object within a $.getJSON function, but I need to be able to use that object outside of the callback.
var jsonIssues = {}; // declare json variable
$.getJSON("url", function(data) {
…

Matt
- 23,363
- 39
- 111
- 152
47
votes
7 answers
Can a function and local variable have the same name?
Here's an example of what I mean:
def foo():
foo = 5
print(foo + 5)
foo()
# => 10
The code doesn't produce any errors and runs perfectly. This contradicts the idea that variables and functions shouldn't have the same name unless you…

jianmin-chen
- 603
- 7
- 9
47
votes
4 answers
Advanced Nested List Comprehension Syntax
I was playing around with list comprehensions to get a better understanding of them and I ran into some unexpected output that I am not able to explain. I haven't found this question asked before, but if it /is/ a repeat question, I apologize.
I was…

inspectorG4dget
- 110,290
- 27
- 149
- 241
47
votes
5 answers
How to use provided scope for jar file in Gradle build?
I need to use Amazon Maps and Amazon Messaging in my apps.
With gradle, I did not succeed in adding the Amazon dependencies with a "provided" scope as they need to be :
The JAR file contains stub implementations of the Amazon Maps API. It does not…

Benjamin
- 2,777
- 2
- 16
- 14