What does scope mean [duplicate]

2019-08-30 07:21发布

问题:

This question already has an answer here:

  • Short description of the scoping rules? 8 answers

From python reference manual:

A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block.

and

When a name is used in a code block, it is resolved using the nearest enclosing scope.

So it is not obviously from this quotes what scope does mean. Is it true that scope is a collection of bindings name-->value? And what does mean enclosing scope? Does it mean that every scope must contain a reference to the enclosing scope?

回答1:

@Martjin Pieters clarifications and answers are awesome, but I'd like to add that beyond python, scope is a computer science/programming concept that spans across basically all programming languages currently in use.

To learn more about what scope is, generally, aside from in python, I'd start here: http://en.wikipedia.org/wiki/Scope_(computer_science)

Many languages, including python, follow the same set of basic scoping rules, but the details can be different between languages. Thus, if you're really asking "what is scope?" then starting at a general source may be more useful than learning the intricacies of python's scoping (at least at first).



标签: python scope