You can simply use the guard feature along with capture:
>>>my_str = "iwill/predict_something"
>>>match my_str:
... case str(x) if 'predict' in x:
... print("match!")
... case _:
... print("nah, dog")
...
match!
Answer from hyp3rg3om3tric on Stack OverflowYou can simply use the guard feature along with capture:
>>>my_str = "iwill/predict_something"
>>>match my_str:
... case str(x) if 'predict' in x:
... print("match!")
... case _:
... print("nah, dog")
...
match!
You could use the [*_] wildcard sequence capture pattern: https://peps.python.org/pep-0622/#sequence-patterns
def is_holiday(yourstring: str):
match yourstring.split():
case [*_, "holidays"]:
return True
case [*_, "workday"]:
return False
print(is_holiday("xmas holidays"))
Videos
Rather than match type(v), match v directly:
values = [
1,
"hello",
True,
]
for v in values:
match v:
case str():
print("It is a string!")
case bool():
print("It is a boolean!")
case int():
print("It is an integer!")
case _:
print(f"It is a {type(v)}!")
Note that I've swapped the order of bool() and int() here, so that True being an instance of int doesn't cause issues.
This is a class pattern match.
You can match directly against the type of v, but you need a value pattern to refer to the types to match, as a "dotless" name is a capture pattern that matches any value. For example,
import builtins
values = [
1,
"hello",
True
]
# Caveat: this will continue to work even if someone
# rebinds the built-in, but not, for example, if builtins.str
# itself is rebound.
for v in values:
match type(v):
case builtins.str:
print("It is a string!")
case builtins.int:
print("It is an integer!")
case builtins.bool:
print("It is a boolean!")
case _:
print(f"It is a {type(v)}!")
Note that a value pattern must be a dotted name; it's not an arbitrary expression that can evaluate to a specific value.
(Whether you really want to match against the actual type of a value, or really want to determine if a value is an instance of a given type, is another matter. In the latter case, an if-elif statement is needed.
if isinstance(v, bool):
print("It is a boolean!")
elif isinstance(v, int):
print("It is an int!")
elif isinstance(v, str):
print("It is a string!")
else:
print(f"It is a {type(v)}!")
There is no pattern that lets you use the result of calling isinstance as the case to match against.
)
I can't say I fully understand the match/case statement yet, but I know a part of it involves more than just simple pattern matching. I know that the expression in the match statement, for example, actually runs and creates a result, such as an object if it's a call to a class.
So I'm wondering, if I don't want to do all that and just want to use it as a cleaner version of if/else, is this considered Pythonic, or is it overkill?
For example:
if some_value == True and some_other == 1:
do something
elif some_value == False and some_other == 1:
do something else
etc etc etcor:
match (some_value, some_other):
case (True, 1):
do something
case (False, 1):
do something else
etc etc etcFirst off, I *think* I'm getting the syntax correct! Second, I'm not actually creating any values with the expressions, so it feels like a glorified if/else construct, just cleaner looking.
Is this a valid use of match/case?
Thanks!
What are your thoughts on it.