Match/case syntax

In the dark ages when I used to code in vbScript I found this form of the select/case to be clearer than repeated if/else if blocks. I there a form of the new match/case in Python that accomplishes the same thing? I am aware that there are easier ways of coding this particular example but the beauty of this form is that the conditions in the case clauses do not have to be related.

select case True
    case distance <= 1.0
    case distance <= 2.0
    case distance <= 3.0
    case distance <= 4.0
    case distance <= 5.0
end select

I do not know vbScript but I think the same code in Python would look like this:

code = ...    # to make syntactically correct code using this word
distance = 3

if distance <= 1.0:
elif distance <= 2.0:
elif distance <= 3.0:
elif distance <= 4.0:
elif distance <= 5.0:

…and this is not the case suited for the match-case statement :slight_smile:

do you mean something like this,

distance = 1

match distance:
    case (int() | float()) as x if x <= 1.0:
        print(f'less than or equal to 1.0')
    case (int() | float()) as x if x <= 2.0:
        print(f'less than or equal to 2.0')
1 Like

That’s more along the line of what I was thinking even if the syntax is a little odd. I understand what that is doing, and why. Personally, I prefer multiple case clauses to repeated elif clauses. One of the nice things about the “select case True” ability of vbScript is that it was more flexible in that the testing was not restricted to just one type of condition. I’m sorry I can’t call up a real-life example at the moment as I haven’t used this construct since I retired in 2008 but a simple example might look like

select case True
    case x = 4
    case y = 19
    case isodd(z)
end select

I see why the new match/case format would not allow something like this, and upon reflection, why my earlier example requires the

case float() as x if x <= 2.0:


I am not sure what is the output of the above code, but you could do something like this,

x, y, z = 4, 19, 1
match (x, y, z):
    case 4, 19, int() as z if z%2!=0:
        print('z is odd, the value of x is 4 and y is 19')
    case 4, _, _:
        print('the value of x is 4')
    case _, 19, _:
        print('the value of y is 19')

The code was not meant as a real life example. A good deal of my pre-retirement code was real-time process control code. I used vbScript for several reasons:

  1. it did not require a complex IDE
  2. it was easily fast enough
  3. on-call staff had access to the code because it was in the production folder tree
  4. it was a simple syntax that everyone already knew

Because there were any number of conditions that had to be checked, depending on previous conditions, the syntax I tried to give an example of was the clearest to code. I’m sorry I do not have a real life example but it’s been too many years. In my first example (distance) I was helping out a Python beginner on another site and the vbScript format seemed more amenable than if/elif. Since the match/case is still pretty new to Python I just wondered if the syntax could be adapted. It looks like the if/elif will be clearer.

For the record, if I had known about Python back then I would easily have used it over vbScript.

Yes, for your examples, if/elif is the right choice.

I was really wondering why you were looking for an overcomplicated solution. You just probably wanted to see something similar to your older solution in vbScript.

Python’s match/case is intended for something different and more complex. If you are interested I recommend reading this: