r/Python • • Mar 19 '21

Match is more than a Switch-Case The New Switch-Case Statement in Python 3.10

https://youtube.com/watch?v=2qJavL-VX9Y&feature=share
1.4k Upvotes

233 comments sorted by

View all comments

Show parent comments

2

u/xigoi Mar 20 '21

How were they impossible before? You could always do

foo = bar
if foo:

instead of

if foo := bar:

1

u/baconcleaner Mar 20 '21

There's always a workaround for everything ;-), but for example with the walrus operator you can assign and use objects directly in nested loops:

class test:
  def __init__(self, v):
    self.v = v + 1

tests = [(obj, obj.v) for x in (11, 22, 33) if (obj := test(x))]
print(tests)