8000 Merge branch 'master' into python-variables-update · realpython/materials@2ed326d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ed326d

Browse files
Merge branch 'master' into python-variables-update
2 parents d679c80 + cad1048 commit 2ed326d

File tree

14 files changed

+183
-2
lines changed

14 files changed

+183
-2
lines changed

python-closure/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Closures: Common Use Cases and Examples
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Closures: Common Use Cases and Examples](https://realpython.com/python-closure/).

python-closure/app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tkinter as tk
2+
3+
app = tk.Tk()
4+
app.title("GUI App")
5+
app.geometry("320x240")
6+
7+
label = tk.Label(app, font=("Helvetica", 16, "bold"))
8+
label.pack()
9+
10+
11+
def callback(text):
12+
def closure():
13+
label.config(text=text)
14+
15+
return closure
16+
17+
18+
button = tk.Button(
19+
app,
20+
text="Greet",
21+
command=callback("Hello, World!"),
22+
)
23+
button.pack()
24+
25+
app.mainloop()

python-closure/appender.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def make_appender():
2+
items = []
3+
4+
def appender(new_item):
5+
items.append(new_item)
6+
return items
7+
8+
return appender

python-closure/closure.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# def outer_func():
2+
# name = "Pythonista"
3+
4+
# def inner_func():
5+
# print(f"Hello, {name}!")
6+
7+
# return inner_func
8+
9+
10+
def outer_func():
11+
name = "Pythonista"
12+
return lambda: print(f"Hello, {name}!")
13+
14+
15+
inner_func = outer_func()
16+
inner_func()

python-closure/counter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def make_counter():
2+
count = 0
3+
4+
def counter():
5+
nonlocal count
6+
count += 1
7+
return count
8+
9+
return counter

python-closure/cum_average.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def cumulative_average():
2+
data = []
3+
4+
def average(value):
5+
data.append(value)
6+
return sum(data) / len(data)
7+
8+
return average

python-closure/decorators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def decorator(function):
2+
def closure():
3+
print("Doing something before calling the function.")
4+
function()
5+
print("Doing something after calling the function.")
6+
7+
return closure
8+
9+
10+
@decorator
11+
def greet():
12+
print("Hi, Pythonista!")

python-closure/free_variables.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def outer_func(outer_arg):
2+
local_var = "Outer local variable"
3+
4+
def closure():
5+
print(outer_arg)
6+
print(local_var)
7+
print(another_local_var)
8+
9+
another_local_var = "Another outer local variable"
10+
11+
return closure

python-closure/inner.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def outer_func():
2+
name = "Pythonista"
3+
4+
def inner_func():
5+
print(f"Hello, {name}!")
6+
7+
inner_func()

python-closure/memoization.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from time import sleep
2+
from timeit import timeit
3+
4+
5+
def memoize(function):
6+
cache = {}
7+
8+
def closure(number):
9+
if number not in cache:
10+
cache[number] = function(number)
11+
return cache[number]
12+
13+
return closure
14+
15+
16+
@memoize
17+
def slow_operation(number):
18+
sleep(0.5)
19+
20+
21+
exec_time = timeit(
22+
"[slow_operation(number) for number in [2, 3, 4, 2, 3, 4]]",
23+
globals=globals(),
24+
number=1,
25+
)
26+
27+
print(f"Slow operation's time: {exec_time:.2f} seconds.")

0 commit comments

Comments
 (0)
0