Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/globa_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)
# globals()["message"] = "Hello, World!"
# print(message)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpozo I assume you commented these out to satisfy the linter checks? In the future, you can use the special # noqa comment to suppress the linter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this doesn't work:

image

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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpozo Same reason for the comment as before?


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(loc)
# number = 42
# print(f"{loc=}")
# loc["message"] = "Welcome!"
# print(f"{loc=}")
# print(locals())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpozo These print calls could also use f-strings to keep them consistent with the updated tutorial.



# 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"]


# f()
# fruits
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpozo This should probably be wrapped in a call to print().

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()