Skip to content

Commit c3560e0

Browse files
committed
tests/basics: Add initial tests for := operator.
1 parent 1b4c6c9 commit c3560e0

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/basics/assign_expr.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(x := 4)
2+
print(x)
3+
4+
if x := 2:
5+
print(True)
6+
print(x)
7+
8+
print(min(4, x := 5))
9+
print(x)
10+
11+
x = 1
12+
print(min(x, x := 5))
13+
print(x)
14+
15+
16+
def foo():
17+
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10)))
18+
return hit
19+
20+
21+
hit = 123
22+
print(foo())
23+
print(hit) # shouldn't be changed by foo
24+
25+
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10)))
26+
print(hit) # should be changed by above
27+
28+
print([i := i + 1 for i in range(4)])
29+
print(i)
30+
del i
31+
32+
print([i := -1 for i, j in [(1, 2)]])
33+
print(i)
34+
35+
print([((m := k + 1), k * m) for k in range(4)])
36+
37+
# j doesn't exist
38+
try:
39+
print([[(j := j) for i in range(2)] for j in range(2)])
40+
except NameError:
41+
print("NameError")
42+
43+
# j is a global
44+
j = 9
45+
print([[(j := j) for i in range(2)] for j in range(2)])
46+
print(j)

tests/basics/assign_expr.py.exp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
4
2+
True
3+
2
4+
4
5+
5
6+
1
7+
5
8+
any True
9+
8
10+
123
11+
any True
12+
8
13+
[1, 2, 3, 4]
14+
4
15+
[-1]
16+
-1
17+
[(1, 0), (2, 2), (3, 6), (4, 12)]
18+
NameError
19+
[[9, 9], [9, 9]]
20+
9

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy