1

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.

coderboy
  • 1,710
  • 1
  • 6
  • 16
  • 1
    I think if a scope is the *context in which a name is available*, then you can reasonably say that a region of code *is* a scope (it is a context in which certain names are available), and that a variable *has* a scope (it has a particular context in which it is available). So *scope* can be region of code and a characteristic of a variable without any contradiction. – khelwood Nov 24 '20 at 09:43
  • 1
    `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.` this is the correct interpretation - or at least closer to correct, close enough. There's nothing on the variable itself that indicates which scope(s) it's part of. It's regions of code which are given access to variables based on various factors. – TKoL Nov 24 '20 at 09:47
  • 2
    But as @khelwood says, it's still useful to be able to talk about which scopes a variable belongs to. Even though the fact of which scopes it's in aren't somehow held IN the variable, you will still talk about `function-scoped` or `block-scoped variables` – TKoL Nov 24 '20 at 09:48
  • If I make the intuition as you guys say, I am able to reason for the terms *local scope* and *global scope*. But what about lexical scope. Where does that fall? – coderboy Nov 24 '20 at 09:50
  • @khelwood I think that a context is different from a scope. A context is the environment available at any instance of evaluating a piece of code. – coderboy Nov 24 '20 at 09:53
  • `lexical scope` is the term for the general concept of Javascript's scoping, it's not a specific scope – TKoL Nov 24 '20 at 09:53
  • If you don't like my choice of the word context, feel free to substitute a synonym with different baggage. – khelwood Nov 24 '20 at 09:56
  • @TKol But resources say that Python functions are lexically-scoped. Or that this JavaScript function refers to its lexical scope. The confusion arises from these ambiguous statements. – coderboy Nov 24 '20 at 09:59
  • 1
    "lexical scope" is a broad and ambiguous term. I think it just means that the scope of something is based on where it's defined in the source code, inside some kind of lexical unit that defines its scope, such as a function definition. – khelwood Nov 24 '20 at 10:02
  • 1
    @TKoL [lexical scope](https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scope) is language agnostic. It's how a lot of the popular scoping techniques work, including JavaScript and Python. [What is lexical scope?](https://stackoverflow.com/q/1047454) It basically means that a variable has access to the scope where it's defined and any scope *upwards*. So, if you have scope `A` and a nested scope `B` a variable in `B` can reference anything in `B` and anything in `A` but a variable in `A` cannot reference things defined in `B` – VLAZ Nov 24 '20 at 10:05
  • @TKoL I think that instead of saying that a language is *lexically-scoped*, we could (and should) say that it implements *lexical variable resolution*. I feel that the term *scope* in *lexical scope* creates the confusion. – coderboy Nov 24 '20 at 10:08
  • 1
    @coderboy I don't think it matters how you say it. Anybody should understand what you mean when you use either of those descriptions. With that said "lexical variable resolution" is overly formal. You may raise some eyebrows. Also, "scope" in "lexical scope" is perfectly fine. When we talk about *scope* it's about where the variable is defined and what it has access to. As a modifier "lexical" (also called "static") means that you can determine what can be accessed before running the code (statically). Dynamic scope is the inverse where the resolution is done at runtime and can vary. – VLAZ Nov 24 '20 at 11:13

0 Answers0