Skip to content

Commit 757644a

Browse files
authored
Merge pull request #675 from realpython/python-scope-legb-rule
Sample code for the article on scope
2 parents 30afa09 + e77c645 commit 757644a

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

python-scope-legb-rule/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Scope and the LEGB Rule: Resolving Names in Your Code
2+
3+
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/).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def square(base):
2+
result = base**2
3+
print(f"The square of {base} is: {result}")
4+
5+
6+
def cube(base):
7+
result = base**3
8+
print(dir())
9+
print(vars())
10+
print(f"The cube of {base} is: {result}")

python-scope-legb-rule/counter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
counter = 0 # A global name
2+
3+
4+
def update_counter_v1():
5+
global counter # Declares counter as global
6+
counter = counter + 1 # Successfully updates the counter
7+
8+
9+
def update_counter_v2(counter):
10+
return counter + 1 # Relies on a local name

python-scope-legb-rule/dispatch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from sys import platform
2+
3+
4+
def linux_print():
5+
print("Printing from Linux...")
6+
7+
8+
def win32_print():
9+
print("Printing from Windows...")
10+
11+
12+
def darwin_print():
13+
print("Printing from macOS...")
14+
15+
16+
printer = globals()[platform + "_print"]
17+
18+
printer()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
numbers = [1, 2, 3]
2+
3+
try:
4+
numbers[4]
5+
except IndexError as error:
6+
# The variable error is local to this block
7+
exception = error
8+
error
9+
10+
11+
print(exception)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def create_lazy_name():
2+
global number # Create a global name lazily
3+
number = 42
4+
return number
5+
6+
7+
create_lazy_name()
8+
print(number)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def outer_func():
2+
variable = 100
3+
4+
def inner_func():
5+
print(f"Printing variable from inner_func(): {variable}")
6+
7+
inner_func()
8+
print(f"Printing variable from outer_func(): {variable}")
9+
10+
11+
outer_func()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def power_factory(exponent):
2+
def power(base):
3+
return base**exponent
4+
5+
return power
6+
7+
8+
square = power_factory(2)
9+
print(square(10))
10+
11+
cube = power_factory(3)
12+
print(cube(10))

0 commit comments

Comments
 (0)