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-namespace/README.md
Original file line number Diff line number Diff line change
@@ -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/).
18 changes: 18 additions & 0 deletions python-namespace/global_stmt.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions python-namespace/globals_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
globals()["message"] = "Welcome to Real Python!"

print(message) # noqa
globals()["message"] = "Hello, World!"
print(message) # noqa
11 changes: 11 additions & 0 deletions python-namespace/legb_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x = "global"


def outer():
def inner():
print(x)

inner()


outer()
13 changes: 13 additions & 0 deletions python-namespace/legb_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
x = "global"


def outer():
x = "enclosing"

def inner():
print(x)

inner()


outer()
14 changes: 14 additions & 0 deletions python-namespace/legb_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
x = "global"


def outer():
x = "enclosing" # noqa

def inner():
x = "local"
print(x)

inner()


outer()
7 changes: 7 additions & 0 deletions python-namespace/local_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def double_number(number):
result = number * 2
print(dir())
return result


double_number(4)
20 changes: 20 additions & 0 deletions python-namespace/locals_func.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions python-namespace/mutability.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions python-namespace/nonlocal_stmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def f():
x = 20

def g():
nonlocal x
x = 40

g()
print(x)


f()
18 changes: 18 additions & 0 deletions python-namespace/scopes.py
Original file line number Diff line number Diff line change
@@ -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()