-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Sample code for the article on namespace #659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
140ed80
b6eadcc
59a616c
1302b53
ca15bd1
6297397
9d59cff
252cfc7
88c9aff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/). |
| 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 |
| 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| x = "global" | ||
|
|
||
|
|
||
| def outer(): | ||
| def inner(): | ||
| print(x) | ||
|
|
||
| inner() | ||
|
|
||
|
|
||
| outer() |
| 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() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| x = "global" | ||
|
|
||
|
|
||
| def outer(): | ||
| # x = "enclosing" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| 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) |
| 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()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lpozo This should probably be wrapped in a call to |
||
| 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() |
| 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() |
There was a problem hiding this comment.
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
# noqacomment to suppress the linter.There was a problem hiding this comment.
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: