Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-scope-legb-rule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python Scope and the LEGB Rule: Resolving Names in Your Code

This folder provides the code examples for the Real Python tutorial [Python Scope and the LEGB Rule: Resolving Names in Your Code](https://realpython.com/python-scope-legb-rule/).
10 changes: 10 additions & 0 deletions python-scope-legb-rule/calculations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def square(base):
result = base**2
print(f"The square of {base} is: {result}")


def cube(base):
result = base**3
print(dir())
print(vars())
print(f"The cube of {base} is: {result}")
10 changes: 10 additions & 0 deletions python-scope-legb-rule/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
counter = 0 # A global name


def update_counter_v1():
global counter # Declares counter as global
counter = counter + 1 # Successfully updates the counter


def update_counter_v2(counter):
return counter + 1 # Relies on a local name
18 changes: 18 additions & 0 deletions python-scope-legb-rule/dispatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sys import platform


def linux_print():
print("Printing from Linux...")


def win32_print():
print("Printing from Windows...")


def darwin_print():
print("Printing from macOS...")


printer = globals()[platform + "_print"]

printer()
11 changes: 11 additions & 0 deletions python-scope-legb-rule/exception_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
numbers = [1, 2, 3]

try:
numbers[4]
except IndexError as error:
# The variable error is local to this block
exception = error
error


print(exception)
8 changes: 8 additions & 0 deletions python-scope-legb-rule/lazy_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def create_lazy_name():
global number # Create a global name lazily
number = 42
return number


create_lazy_name()
print(number)
11 changes: 11 additions & 0 deletions python-scope-legb-rule/nested_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def outer_func():
variable = 100

def inner_func():
print(f"Printing variable from inner_func(): {variable}")

inner_func()
print(f"Printing variable from outer_func(): {variable}")


outer_func()
12 changes: 12 additions & 0 deletions python-scope-legb-rule/power_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def power_factory(exponent):
def power(base):
return base**exponent

return power


square = power_factory(2)
print(square(10))

cube = power_factory(3)
print(cube(10))