-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Sample code for the article on closures #581
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a408601
Sample code for the article on closures
lpozo cead9e4
TR updates, first round
lpozo 9d11980
Remove usage code
lpozo 625c2c1
Merge branch 'master' into python-closure
lpozo 4df1586
Merge branch 'master' into python-closure
lpozo a6c7824
TR updates, first round
lpozo 7f20858
Merge branch 'master' into python-closure
brendaweles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Python Closures: Common Use Cases and Examples | ||
|
||
This folder provides the code examples for the Real Python tutorial [Python Closures: Common Use Cases and Examples](https://realpython.com/python-closure/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import tkinter as tk | ||
|
||
app = tk.Tk() | ||
app.title("GUI App") | ||
app.geometry("320x240") | ||
|
||
label = tk.Label(app, font=("Helvetica", 16, "bold")) | ||
label.pack() | ||
|
||
|
||
def callback(text): | ||
def closure(): | ||
label.config(text=text) | ||
|
||
return closure | ||
|
||
|
||
button = tk.Button( | ||
app, | ||
text="Greet", | ||
command=callback("Hello, World!"), | ||
) | ||
button.pack() | ||
|
||
app.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def make_appender(): | ||
items = [] | ||
|
||
def appender(new_item): | ||
items.append(new_item) | ||
return items | ||
|
||
return appender |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# def outer_func(): | ||
# name = "Pythonista" | ||
|
||
# def inner_func(): | ||
# print(f"Hello, {name}!") | ||
|
||
# return inner_func | ||
|
||
|
||
def outer_func(): | ||
name = "Pythonista" | ||
return lambda name=name: print(f"Hello, {name}!") | ||
|
||
|
||
inner_func = outer_func() | ||
inner_func() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def make_counter(): | ||
count = 0 | ||
|
||
def counter(): | ||
nonlocal count | ||
count += 1 | ||
return count | ||
|
||
return counter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def cumulative_average(): | ||
data = [] | ||
|
||
def average(value): | ||
data.append(value) | ||
return sum(data) / len(data) | ||
|
||
return average |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def decorator(function): | ||
def closure(): | ||
print("Doing something before calling the function.") | ||
function() | ||
print("Doing something after calling the function.") | ||
|
||
return closure | ||
|
||
|
||
@decorator | ||
def greet(): | ||
print("Hi, Pythonista!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def outer_func(outer_arg): | ||
local_var = "Outer local variable" | ||
|
||
def closure(): | ||
print(outer_arg) | ||
print(local_var) | ||
print(another_local_var) | ||
|
||
another_local_var = "Another outer local variable" | ||
|
||
return closure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def outer_func(): | ||
name = "Pythonista" | ||
|
||
def inner_func(): | ||
print(f"Hello, {name}!") | ||
|
||
inner_func() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from time import sleep | ||
from timeit import timeit | ||
|
||
|
||
def memoize(function): | ||
cache = {} | ||
|
||
def closure(number): | ||
if number not in cache: | ||
cache[number] = function(number) | ||
return cache[number] | ||
|
||
return closure | ||
|
||
|
||
@memoize | ||
def slow_operation(number): | ||
sleep(0.5) | ||
|
||
|
||
exec_time = timeit( | ||
"[slow_operation(number) for number in [2, 3, 4, 2, 3, 4]]", | ||
globals=globals(), | ||
number=1, | ||
) | ||
|
||
print(f"Slow operation's time: {exec_time:.2f} seconds.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
def make_root_calculator(root_degree, precision=2): | ||
def root_calculator(number): | ||
return round(pow(number, 1 / root_degree), precision) | ||
|
||
return root_calculator | ||
|
||
|
||
square_root = make_root_calculator(2, 4) | ||
print(square_root(42)) | ||
|
||
cubic_root = make_root_calculator(3, 2) | ||
print(cubic_root(42)) | ||
|
||
|
||
class RootCalculator: | ||
def __init__(self, root_degree, precision=2): | ||
self.root_degree = root_degree | ||
self.precision = precision | ||
|
||
def __call__(self, number): | ||
return round(pow(number, 1 / self.root_degree), self.precision) | ||
|
||
|
||
square_root = RootCalculator(2, 4) | ||
print(square_root(42)) | ||
|
||
cubic_root = RootCalculator(3, 2) | ||
print(cubic_root(42)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Stack: | ||
def __init__(self): | ||
self._items = [] | ||
|
||
def push(self, item): | ||
self._items.append(item) | ||
|
||
def pop(self): | ||
return self._items.pop() | ||
|
||
def __str__(self): | ||
return str(self._items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def Stack(): | ||
_items = [] | ||
|
||
def push(item): | ||
_items.append(item) | ||
|
||
def pop(): | ||
return _items.pop() | ||
|
||
def closure(): | ||
pass | ||
|
||
closure.push = push | ||
closure.pop = pop | ||
return closure |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 Actually, I just realized this is the wrong syntax for a lambda expression. This is the correct one:
Unfortunately, this mistake is also present in the tutorial text.
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.
It doesn't work if we don't set the argument:
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.
The correct syntax is: