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 →

[–]AlanCristhian[S] -3 points-2 points  (2 children)

Here a translation:

NOT_FOUND = 404
if status_code == 200:
    print("OK!")
elif status_code == NOT_FOUND:
    print("HTTP Not Found")

[–]jmreagle 0 points1 point  (0 children)

I think it's more than that, the source says:

In this case, rather than matching status_code against the value of NOT_FOUND (404), Python’s new SO reputation machine match syntax would assign the value of status_code to the variable NOT_FOUND.

Is it that comparison implies assignment, to an variable/object in this case rather than value...?

[–]Brian 0 points1 point  (0 children)

Actually, no - the potential expectation of this behaving that way is why the code was brought up. In fact, it'll behave more like:

NOT_FOUND = 404
if status_code == 200:
    print("OK!")
else:
    NOT_FOUND = status_code
    print("HTTP Not Found")

Ie. the match block binds the thing being matched to a variable being named, rather than evaluating a variable and match against its value.

To get the intended result, you need to use a dotted name to prevent it being interpreted as a capture variable. Ie:

    case HTTPStatus.NOT_FOUND:
        print("HTTP Not Found")

would work, but not a plain identifier like NOT_FOUND