Scoping associates a name with an entity. For an object to be *in scope* means that it is possible to write a snippet of code referencing value and location of that object by its name. Lexical scoping refers to a proportion of text whereas dynamic scoping corresponds to the proportion of run time.
Questions tagged [scoping]
584 questions
                    
                    844
                    
            votes
                
                22 answers
            
        
                    183
                    
            votes
                
                7 answers
            
        How do you use "<<-" (scoping assignment) in R?
I just finished reading about scoping in the R intro, and am very curious about the <<- assignment.
The manual showed one (very interesting) example for <<-, which I feel I understood. What I am still missing is the context of when this can be…
         
    
    
        Tal Galili
        
- 24,605
- 44
- 129
- 187
                    103
                    
            votes
                
                18 answers
            
        Javascript function scoping and hoisting
I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example: 
var a = 1;
function b() {
    a = 10;
    return;
    function a() {}
}
b();
alert(a);
Using the code above, the browser…
         
    
    
        dev.e.loper
        
- 35,446
- 76
- 161
- 247
                    103
                    
            votes
                
                7 answers
            
        Node-style require for in-browser javascript?
Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use as Node's require?
To provide more detail: the reason require is so good is that it:
Allows code to be dynamically loaded from other…
         
    
    
        Alex Churchill
        
- 4,887
- 5
- 30
- 42
                    76
                    
            votes
                
                16 answers
            
        What is the difference between my and local in Perl?
I am seeing both of them used in this script I am trying to debug and the literature is just not clear. Can someone demystify this for me?
         
    
    
        Brian G
        
- 53,704
- 58
- 125
- 140
                    58
                    
            votes
                
                1 answer
            
        Is it a good practice to call functions in a package via ::
I'm writing some R functions that employ some useful functions in other packages like stringr and base64enc. Is it good not to call library(...) or require(...) to load these packages first but to use :: to directly refer to the function I need,…
         
    
    
        Kun Ren
        
- 4,715
- 3
- 35
- 50
                    44
                    
            votes
                
                3 answers
            
        "except Foo as bar" causes "bar" to be removed from scope
Given the following code:
msg = "test"
try:
    "a"[1]
except IndexError as msg:
    print("Error happened")
print(msg)
Can somebody explain why this causes the following output in Python 3?
Error happened
Traceback (most recent call last):
  File…
         
    
    
        knipknap
        
- 5,934
- 7
- 39
- 43
                    43
                    
            votes
                
                2 answers
            
        What are the double colons (::) in R?
I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use. 
Here is an example of their use.
df <- dplyr::data_frame(
  year = c(2015, NA, NA, NA), 
  trt = c("A",…
         
    
    
        Luis Candanedo
        
- 907
- 2
- 9
- 12
                    32
                    
            votes
                
                7 answers
            
        Are variables statically or dynamically "scoped" in javascript?
Or more specific to what I need:
If I call a function from within another function, is it going to pull the variable from within the calling function, or from the level above? Ex:
myVar=0;
function runMe(){
    myVar = 10;
    callMe();
}
function…
         
    
    
        kylex
        
- 14,178
- 33
- 114
- 175
                    31
                    
            votes
                
                2 answers
            
        Python class scoping rules
EDIT: Looks like this is a very old "bug" or, actually, feature. See, e.g., this mail 
I am trying to understand the Python scoping rules. More precisely, I thought that I understand them but then I found this code here: 
x = "xtop"
y = "ytop"
def…
         
    
    
        ivanl
        
- 741
- 7
- 17
                    30
                    
            votes
                
                3 answers
            
        Dynamic Scoping - Deep Binding vs Shallow Binding
I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with
a) deep binding
b)…
         
    
    
        John Jiang
        
- 11,069
- 12
- 51
- 60
                    30
                    
            votes
                
                1 answer
            
        How can a non-imported method in a not-attached package be found by calls to functions not having it in their namespace?
An R namespace acts as the immediate environment for all functions in its associated package. In other words, when function bar() from package foo calls another function, the R evaluator first searches for the other function in 
        
            
            
                
                    
    
    
         
    
    
                
            
        
     
 
    
    
        Josh O'Brien
        
- 159,210
- 26
- 366
- 455
                    23
                    
            votes
                
                3 answers
            
        Why use a do-end block in Lua?
I keep trying to find answers for this but fail to do so. I wanted to know, what is the do-end block actually used for? It just says values are used when needed in my book so how could I use this?
Do I use it to reduce the scope of local variables…
         
    
    
        Mayron
        
- 2,146
- 4
- 25
- 51
                    21
                    
            votes
                
                4 answers
            
        attach() inside function
I'd like to give a params argument to a function and then attach it so that I can use a instead of params$a everytime I refer to the list element a.
run.simulation<-function(model,params){
attach(params)
#
# Use elements of params as parameters in a…
         
    
    
        statistician_in_training
        
- 803
- 1
- 7
- 18
                    21
                    
            votes
                
                3 answers
            
        could not find function inside foreach loop
I'm trying to use foreach to do multicore computing in R. 
A <-function(....) {
    foreach(i=1:10) %dopar% {
    B()
    }
}
then I call function A in the console. The problem is I'm calling a function Posdef inside B that is defined in another…
         
    
    
        statistician_in_training
        
- 803
- 1
- 7
- 18
