Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scope (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Lexical scope vs. dynamic scope === A fundamental distinction in scope is what "part of a program" means. In languages with '''lexical scope''' (also called '''static scope'''), name resolution depends on the location in the source code and the ''lexical context'' (also called ''static context''), which is defined by where the named variable or function is defined. In contrast, in languages with '''dynamic scope,''' the name resolution depends upon the [[program state]] when the name is encountered which is determined by the ''execution context'' (also called ''runtime context'', ''calling context'' or ''dynamic context''). In practice, with lexical scope a name is resolved by searching the local lexical context, then if that fails, by searching the outer lexical context, and so on; whereas with dynamic scope, a name is resolved by searching the local execution context, then if that fails, by searching the outer execution context, and so on, progressing up the call stack.<ref name=Borning /> Most modern languages use lexical scope for variables and functions, though dynamic scope is used in some languages, notably some dialects of Lisp, some "scripting" languages, and some [[template language]]s.{{efn|1=For example, the [[Jinja (template engine)|Jinja]] template engine for Python by default uses both lexical scope (for imports) and dynamic scope (for includes), and allows behavior to be specified with keywords; see [http://jinja.pocoo.org/docs/templates/#import-context-behavior Import Context Behavior].}} Perl 5 offers both lexical and dynamic scope. Even in lexically scoped languages, scope for [[Closure (computer science)|closures]] can be confusing to the uninitiated,{{citation needed|date=March 2022}} as these depend on the lexical context where the closure is defined, not where it is called. Lexical resolution can be determined at [[compile time]], and is also known as ''early binding'', while dynamic resolution can in general only be determined at [[Run time (program lifecycle phase)|run time]], and thus is known as ''late binding''.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)