I have read a lot on the idea of scoping in a programming language. But whatever, I've read, there doesn't seem to be a strict definition of the term.
I don't understand whether a scope is a characteristic of a variable for e.g x is a global variable, y is a local variable.
Or whether a scope refers to a region of a program where given variables are available for e.g this is the global scope, this is the local scope.
The confusion arises because I have read both the above sentences in books, blogs, and tutorials.
For e.g in JavaScript, let
creates a block scope. Now to me, both the following sentences given as comments seem correct:
{
let x = 10; // this is a block-scoped variable
// this is a block scope
}
The first comment says that x is a block-scoped variable i.e it refers to a characteristic of the variable that it has a block scope.
The second one says that the whole region inside the block is a block scope, which implies that a scope is a region of code.
I don't know which one is strictly and technically correct, or just more appropriate.
Moving on, we know that Python is a lexically-scoped language. Taking the following Python code as an example, which sentence would be correct to say:
x = 10
def foo():
print(x)
Sentence 1: The function foo()
has a lexical-scope.
Sentence 2: The lexical scope of the function foo()
has the variable x
.
Sentence 3: The variable x
is lexically-scoped.
I am just confused in what is correct to say when using the term scope. If I stick to the fact, that scope is a characteristic of variables then sentence 1 and 2 above would be incorrect, however they don't seem incorrect.
I am not confused as to how variable resolution happens in Python, or other such languages, whereby a name is resolved based on the lexical context. I am only confused about the term scope.