Scope

In Python, the concept of scope determines where a variable can be accessed within the code. A variable’s scope defines its visibility and lifespan, and it is crucial to understand these boundaries to manage variables effectively.

Local Scope

A variable defined inside a function is said to have local scope. This means that the variable is only accessible within the function where it was created.

def myfunc():
  x = "The Hobbit"
  print(x)

myfunc()
# Expected output: The Hobbit

In the example above, the variable x is local to myfunc and cannot be accessed outside of this function. However, it can be accessed by any nested functions within myfunc.

Function Inside Function

Local variables can be accessed by functions defined within the function where the variable is declared:

def myfunc():
  x = "1984"
  def myinnerfunc():
    print(x)
  myinnerfunc()

myfunc()
# Expected output: 1984

Here, the variable x is accessible within myinnerfunc because it is defined in the enclosing function myfunc.

Global Scope

A variable defined outside of all functions is referred to as a global variable. Global variables are accessible from anywhere within the script, including within functions.

x = "Brave New World"

def myfunc():
  print(x)

myfunc()
print(x)
# Expected output: 
# Brave New World
# Brave New World

In this example, the variable x is declared globally and can be accessed both inside and outside the myfunc function.

Naming Variables

If you use the same variable name in both a global and local context, Python treats them as distinct variables. The local variable overshadows the global one within its scope:

x = "To Kill a Mockingbird"

def myfunc():
  x = "War and Peace"
  print(x)

myfunc()
print(x)
# Expected output: 
# War and Peace
# To Kill a Mockingbird

Here, myfunc prints the local x (“War and Peace”), while the global x (“To Kill a Mockingbird”) is printed outside the function.

Using the global Keyword

To modify a global variable from within a function, you can use the global keyword to declare that you are working with the global variable, not creating a new local one:

def myfunc():
  global x
  x = "Moby Dick"

myfunc()
print(x)
# Expected output: Moby Dick

Using global ensures that changes to the variable are applied globally. This keyword is also necessary if you want to update a global variable within a function:

x = "Pride and Prejudice"

def myfunc():
  global x
  x = "Ulysses"

myfunc()
print(x)
# Expected output: Ulysses

The variable x is changed globally from “Pride and Prejudice” to “Ulysses” by the function myfunc.

Using the nonlocal Keyword

The nonlocal keyword is used to work with variables in nested functions. It allows you to refer to a variable in the nearest enclosing scope that is not global:

def myfunc1():
  x = "The Catcher in the Rye"
  def myfunc2():
    nonlocal x
    x = "The Great Gatsby"
  myfunc2()
  return x

print(myfunc1())
# Expected output: The Great Gatsby

In this example, nonlocal allows myfunc2 to modify x in myfunc1, so the returned value is “The Great Gatsby”.

Leave a Comment

Fhd dual led quantum hdr smart tv review tapreneur. Alphonso henry is a tech and smart home expert passionate about simplifying home automation for everyone.