Skip to content

Commit 140ed80

Browse files
committed
Sample code for the article on namespace
1 parent a0f3307 commit 140ed80

File tree

12 files changed

+156
-0
lines changed

12 files changed

+156
-0
lines changed

python-namespace/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Namespaces and Scope in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Namespaces and Scope in Python](https://realpython.com/python-namespaces-scope/).

python-namespace/globa_stmt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
x = 20
2+
3+
4+
def f():
5+
global x
6+
x = 40
7+
print(x)
8+
9+
10+
f()
11+
print(x)
12+
13+
14+
# x, y, z = 10, 20, 30
15+
16+
17+
# def f():
18+
# global x, y, z

python-namespace/globals_func.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
globals()["message"] = "Welcome to Real Python!"
2+
3+
print(message)
4+
globals()["message"] = "Hello, World!"
5+
print(message)

python-namespace/legb_1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
x = "global"
2+
3+
4+
def outer():
5+
def inner():
6+
print(x)
7+
8+
inner()
9+
10+
11+
outer()

python-namespace/legb_2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
x = "global"
2+
3+
4+
def outer():
5+
x = "enclosing"
6+
7+
def inner():
8+
print(x)
9+
10+
inner()
11+
12+
13+
outer()

python-namespace/legb_3.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
x = "global"
2+
3+
4+
def outer():
5+
x = "enclosing"
6+
7+
def inner():
8+
x = "local"
9+
print(x)
10+
11+
inner()
12+
13+
14+
outer()

python-namespace/legb_4.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def outer():
2+
def inner():
3+
print(x)
4+
5+
inner()
6+
7+
8+
outer()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def double_number(number):
2+
result = number * 2
3+
print(dir())
4+
return result
5+
6+
7+
double_number(4)

python-namespace/locals_func.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def func(x, y):
2+
message = "Hello!"
3+
print(locals())
4+
5+
6+
func(10, 0.5)
7+
8+
9+
# def func():
10+
# message = "Hello!"
11+
# loc = locals()
12+
# print(loc)
13+
# number = 42
14+
# print(f"{loc=}")
15+
# loc["message"] = "Welcome!"
16+
# print(f"{loc=}")
17+
# print(locals())
18+
19+
20+
# func()

python-namespace/mutability.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
x = 20
2+
3+
4+
def f():
5+
x = 40
6+
print(x)
7+
8+
9+
f()
10+
print(x)
11+
12+
13+
# fruits = ["apple", "banana", "cherry", "mango"]
14+
15+
16+
# def f():
17+
# fruits[1] = "peach"
18+
19+
20+
# f()
21+
# print(fruits)
22+
23+
24+
# fruits = ["apple", "banana", "cherry", "mango"]
25+
26+
27+
# def f():
28+
# fruits = ["grapes", "orange"]
29+
30+
31+
# f()
32+
# fruits

0 commit comments

Comments
 (0)