diff --git a/python-namespace/README.md b/python-namespace/README.md new file mode 100644 index 0000000000..7cee452d76 --- /dev/null +++ b/python-namespace/README.md @@ -0,0 +1,3 @@ +# Namespaces and Scope in Python + +This folder provides the code examples for the Real Python tutorial [Namespaces and Scope in Python](https://realpython.com/python-namespaces-scope/). diff --git a/python-namespace/global_stmt.py b/python-namespace/global_stmt.py new file mode 100644 index 0000000000..36ffeb16ce --- /dev/null +++ b/python-namespace/global_stmt.py @@ -0,0 +1,18 @@ +x = 20 + + +def f(): + global x + x = 40 + print(x) + + +f() +print(x) + + +x, y, z = 10, 20, 30 + + +def f(): + global x, y, z diff --git a/python-namespace/globals_func.py b/python-namespace/globals_func.py new file mode 100644 index 0000000000..b4d84faf18 --- /dev/null +++ b/python-namespace/globals_func.py @@ -0,0 +1,5 @@ +globals()["message"] = "Welcome to Real Python!" + +print(message) # noqa +globals()["message"] = "Hello, World!" +print(message) # noqa diff --git a/python-namespace/legb_1.py b/python-namespace/legb_1.py new file mode 100644 index 0000000000..1561e295e9 --- /dev/null +++ b/python-namespace/legb_1.py @@ -0,0 +1,11 @@ +x = "global" + + +def outer(): + def inner(): + print(x) + + inner() + + +outer() diff --git a/python-namespace/legb_2.py b/python-namespace/legb_2.py new file mode 100644 index 0000000000..2a6cf87ec0 --- /dev/null +++ b/python-namespace/legb_2.py @@ -0,0 +1,13 @@ +x = "global" + + +def outer(): + x = "enclosing" + + def inner(): + print(x) + + inner() + + +outer() diff --git a/python-namespace/legb_3.py b/python-namespace/legb_3.py new file mode 100644 index 0000000000..2bfb146357 --- /dev/null +++ b/python-namespace/legb_3.py @@ -0,0 +1,14 @@ +x = "global" + + +def outer(): + x = "enclosing" # noqa + + def inner(): + x = "local" + print(x) + + inner() + + +outer() diff --git a/python-namespace/local_variables.py b/python-namespace/local_variables.py new file mode 100644 index 0000000000..ab1027290f --- /dev/null +++ b/python-namespace/local_variables.py @@ -0,0 +1,7 @@ +def double_number(number): + result = number * 2 + print(dir()) + return result + + +double_number(4) diff --git a/python-namespace/locals_func.py b/python-namespace/locals_func.py new file mode 100644 index 0000000000..c208db99c1 --- /dev/null +++ b/python-namespace/locals_func.py @@ -0,0 +1,20 @@ +def func(x, y): + message = "Hello!" + print(locals()) + + +func(10, 0.5) + + +def func(): + message = "Hello!" + loc = locals() + print(f"{loc = }") + number = 42 + print(f"{loc = }") + loc["message"] = "Welcome!" + print(f"{loc = }") + print(f"{locals() = }") + + +func() diff --git a/python-namespace/mutability.py b/python-namespace/mutability.py new file mode 100644 index 0000000000..3603773f5a --- /dev/null +++ b/python-namespace/mutability.py @@ -0,0 +1,32 @@ +x = 20 + + +def f(): + x = 40 + print(x) + + +f() +print(x) + + +fruits = ["apple", "banana", "cherry", "mango"] + + +def f(): + fruits[1] = "peach" + + +f() +print(fruits) + + +fruits = ["apple", "banana", "cherry", "mango"] + + +def f(): + fruits = ["grapes", "orange"] # noqa + + +f() +print(fruits) diff --git a/python-namespace/nonlocal_stmt.py b/python-namespace/nonlocal_stmt.py new file mode 100644 index 0000000000..a65c8abdc0 --- /dev/null +++ b/python-namespace/nonlocal_stmt.py @@ -0,0 +1,12 @@ +def f(): + x = 20 + + def g(): + nonlocal x + x = 40 + + g() + print(x) + + +f() diff --git a/python-namespace/scopes.py b/python-namespace/scopes.py new file mode 100644 index 0000000000..5c95c95a0b --- /dev/null +++ b/python-namespace/scopes.py @@ -0,0 +1,18 @@ +global_variable = "global" + + +def outer_func(): + # Nonlocal scope + nonlocal_variable = "nonlocal" + + def inner_func(): + # Local scope + local_variable = "local" + print(f"Hi from the '{local_variable}' scope!") + print(f"Hi from the '{nonlocal_variable}' scope!") + print(f"Hi from the '{global_variable}' scope!") + + inner_func() + + +outer_func()