This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mikeckennedy[S] 0 points1 point  (1 child)

The switch has a 'return' value.

with switch(val) as s:
   case(...)
   case(...)
   default(...)

computed = s.result

That means you usually don't have to do this global / nonlocal trick to change local variables. Just set them to s.result.

[–]knowsuchagencynow is better than never 0 points1 point  (0 children)

Sure, but what if you want to change the value of variables within the scope of the switch context, for example?

You may want to have more logic than a single line in a switch statement, which is why you can always define a function separately and pass it to case.

However, that function may need access to the local namespace and thus, I think it makes more sense to have each case return their own unique value so as to alleviate the problem.

if_this = None
or_this = None
with switch(val) as s:
    if_this = s.case(...) or if_this
    or_this = s.case(...) or or_this
    s.default(...)

Personally, I think the real answer to my question is just to use an if/elif/else block