Python
Tips
Checking Specific Instances of classes using match()
class Entity:
...
class Human(Entity):
...
joe = Human()
obj = Entity()
match joe:
case _ if type(joe) is Entity:
print("It should not evaluate to this case")
case _ if type(joe) is Human:
print("It should evaluate to this case")
case _:
print("It won't.")
match obj:
case _ if type(joe) is Human:
print("It should evaluate to this case")
case _ if type(joe) is Entity:
print("It should not evaluate to this case")
case _:
print("Another test to confirm")
_ if is used for extending custom conditions since pattern matching does not work for booleans.
Command-line arguments
import sys args = sys.argv
sys.args will always contain the filename
better use argparse for parameters with arguments
import argparse
parser = argparse.ArgumentParser(description="description here")
parser.add_argument("--name", help="set name")
args = parser.parse_args()
to use flags, add action="store_true" inside the argument. default values can also be set with default="value".
metavar for changing help argument string