-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalrus_operator.py
74 lines (54 loc) · 2.16 KB
/
walrus_operator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Walrus Operator
# Hello, Walrus!
# To get a first impression of what assignment expressions are all about,
# start your REPL and play around with the following code:
walrus = False
print(walrus)
(walrus := True)
print(walrus)
# Dictionaries
numbers = [2, 8, 0, 1, 1, 9, 7, 7]
'''Note that both the sum and the length of the numbers list are calculated twice.'''
description = {
"length": len(numbers),
"sum": sum(numbers),
"mean": sum(numbers) / len(numbers),
}
print(description)
'''The variables num_length and num_sum are only used to optimize the calculations
inside the dictionary. By using the walrus operator, you can make this role clearer'''
description = {
"length": (num_length := len(numbers)),
"sum": (num_sum := sum(numbers)),
"mean": num_sum / num_length,
}
print(description)
# List
'''Here, you filter the numbers list and leave the positive results from applying slow().
The problem with this code is that this expensive function is called twice.
'''
numbers = [7, 6, 1, 4, 1, 8, 0, 6]
def slow(x):
return x-2
results = [slow(num) for num in numbers if slow(num) > 0]
print(results)
'''You can rewrite the list comprehension using the walrus operator as follows,
Note that the parentheses around value := slow(num) are required.
This version is effective and readable, and it communicates the intent of the code well.'''
results = [value for num in numbers if (value := slow(num)) > 0]
print(results)
'''Here, you first capture all city names that start with "B".
Then, if there’s at least one such city name, you print out the
first city name starting with "B".'''
cities = ['Monaco', 'Vancouver', 'Rio de Janeiro', 'Berlin', 'La Paz', 'Bogota', 'Dallas']
if any((witness := city).startswith("B") for city in cities):
print(f"{witness} starts with B")
else:
print("No city name starts with B")
'''You can more clearly see what’s happening by wrapping .startswith("B") in a function that
also prints out which item is being checked:'''
def starts_with_b(name):
print(f"Checking {name}: {(result := name.startswith('B'))}")
return result
any(starts_with_b(city) for city in cities)
all(starts_with_b(city) for city in cities)