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
for line in open(filename): | |
do_something(line) |
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
def basic_generator(): | |
for i in range(50_000_000): | |
yield i | |
gen = basic_generator() | |
for i in gen: | |
print(i) |
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
def round_pows_slow(): | |
return [pow(x, 2) for x in range(3_000_000) if pow(x, 2) % 2 == 0] | |
def round_pows_fast(): | |
return [i for x in range(3_000_000) if (i := pow(x, 2) % 2 == 0)] | |
>>> round_pows_slow() runtime: 1105.8068 ms | |
>>> round_pows_fast() runtime: 899.7900 ms | |
>>> round_pows_fast() improvement: 206.0168 ms |
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
def round_squares_slow(): | |
return [pow(x, 2) for x in range(3_000_000) if pow(x, 2) % 2 == 0] | |
def round_squares_fast(): | |
return [i for x in range(3_000_000) if (i := pow(x, 2) % 2 == 0)] | |
>>> round_squares_slow() runtime: 1105.806827545166 ms | |
>>> round_squares_fast() runtime: 899.7900485992432 ms | |
>>> round_squares_fast() improvement: 206.01677894592285 ms |
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
# Scope of variable without assignment expression. | |
def say_hello(): | |
name = get_random_name() | |
if name: | |
print(f"Hello, {name}") | |
print(f"name {name} is still in scope") | |
# Scope of variable using assignment expression. | |
def say_hello(): | |
if name := get_random_name(): |
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
# Top level of an expression statement | |
y := f(x) | |
# At the top level of the right hand side of an expression statement. | |
y0 = y1 := f(x) | |
# Value of a keyword argument in a function call. | |
f(x = y := f(x)) | |
# Default value in a function definition. |
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
my_dict = {"age": 28} | |
if val := my_dict.get("name"): # evaluates to False so `print(val)` is not executed. | |
print(val) | |
if val := my_dict.get("age"): # evaluates to True so `print(val)` is executed. | |
print(val) |
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
import dis | |
def add(x, y): | |
return x + y | |
>>> dis.dis(add) | |
2 0 LOAD_FAST 0 (x) | |
2 LOAD_FAST 1 (y) | |
4 BINARY_ADD |
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
2 0 LOAD_GLOBAL 0 (print) | |
2 LOAD_CONST 1 ('Hello, World!') | |
4 CALL_FUNCTION 1 | |
6 POP_TOP | |
8 LOAD_CONST 0 (None) | |
10 RETURN_VALUE |
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
import dis | |
def hello_world(): | |
print("Hello, World!") | |
>>> dis.dis(hello_world) | |
2 0 LOAD_GLOBAL 0 (print) | |
2 LOAD_CONST 1 ('Hello, World!') |