Skip to content

Commit 6caca3e

Browse files
authored
Merge branch 'master' into python-uv
2 parents fd8ac39 + 13dd736 commit 6caca3e

File tree

11 files changed

+153
-0
lines changed

11 files changed

+153
-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/global_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) # noqa
4+
globals()["message"] = "Hello, World!"
5+
print(message) # noqa

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" # noqa
6+
7+
def inner():
8+
x = "local"
9+
print(x)
10+
11+
inner()
12+
13+
14+
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(f"{loc = }")
13+
number = 42
14+
print(f"{loc = }")
15+
loc["message"] = "Welcome!"
16+
print(f"{loc = }")
17+
print(f"{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"] # noqa
29+
30+
31+
f()
32+
print(fruits)

python-namespace/nonlocal_stmt.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def f():
2+
x = 20
3+
4+
def g():
5+
nonlocal x
6+
x = 40
7+
8+
g()
9+
print(x)
10+
11+
12+
f()

0 commit comments

Comments
 (0)