I'm experiencing this behavior in PyCharm Build #PY-222.4345.23
on macOS Monterey 12.6.1
, which happens in both python 3.10
and 3.11
.
def example(i):
match i % 4:
case 0:
if i > 10:
return 0
case 1:
if i > 10:
return 1
case 2:
if i > 10:
print(f'{i}, {i > 10}')
return 2
case 3:
if i > 10:
return 3
if __name__ == '__main__':
print(example(2))
In both python versions, debugger stops at 3rd return statement (unexpected, block should be unreachable) but doesn't actually return anything which is the expected behavior for the given input 2
python 3.11 (1 breakpoint to reproduce)
python 3.10 (2 breakpoints to reproduce)
If switch and cases were replaced with if
/elif
blocks, the very same breakpoint is never reached which is exactly the behavior I'm expecting for the switch blocks.
def example(i):
j = i % 4
if j == 0:
if i > 10:
return 0
elif j == 1:
if i > 10:
return 1
elif j == 2:
if i > 10:
print(f'{i}, {i > 10}')
return 2
elif j == 3:
if i > 10:
return 3
if __name__ == '__main__':
print(example(2))
return 2
. If you continue (e.g. line by line), the debugger will execute thereturn
and so you will have the2
. But on your code,return 2
should never be executed. You callreturn 2
only ifi > 10
, but you havei = 2
, so no return. Please specify better what do you expect and what you get.i = 2
.example(14)
and not forexample(2)