Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
37 lines (27 loc) · 1.12 KB

File metadata and controls

37 lines (27 loc) · 1.12 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Variable Declarations

Declaring a variable is simply a matter of just assigning a value to a variable identifier:

a = 10

Block-scoping

When a variable is declared with an assignment, it uses what some call lexical-scoping or block-scoping. Block-scoped variables are not visible outside of their nearest containing block or for-loop.

def f(inp):
    a = 100

    if inp:
        # Still okay to reference 'a'
        b = a + 1
        return b

    # Error: 'b' doesn't exist here
    return b

Here, we have two local variables a and b. a's scope is limited to the body of f while b's scope is limited to the containing if statement's block.

Another property of block-scoped variables is that they can't be read or written to before they're actually declared. While these variables are "present" throughout their scope, all points up until their declaration are part of their temporal dead zone. This is just a sophisticated way of saying you can't access them before the first time they are assigned.

a++ # illegal to use 'a' before it's declared
a = 0
Morty Proxy This is a proxified and sanitized view of the page, visit original site.