this is my code
class BasePlayer:
@staticmethod
def walk(direction):
print(f"I run into {direction}")
class Archer(BasePlayer):
@staticmethod
def shoot():
print("shoot")
class Wizard(BasePlayer):
@staticmethod
def cast_spell():
print("Spell casted")
def play(expr):
match expr.split():
case [("Archer" | "Wizard") as player, "walk", ("north" | "south" | "west" | "east") as direction]:
Archer.walk(direction)
case ["Archer", "shoot"]:
Archer.shoot()
case _:
raise Exception("Command not working...")
play("Archer walk west")
That works fine so far, but I want in the first case
statement, I want so do it more generic like this:
case [(Archer | Wizard) as player, "walk", ("north" | "south" | "west" | "east") as direction]:
player.walk(direction)
However I can not use Objects in the case statements (pattern does not bind Archer). If I leave it as a string, player
will not be the class, but also just a string.
Is there a way to make this work?