Created
May 10, 2023 20:17
-
-
Save JacobCallahan/a5ac0a0e09039bf648eedeafa1f65a7a to your computer and use it in GitHub Desktop.
a stupid class that makes variable access in structural pattern matching less painful
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SPMHelper: | |
def __init__(self, **vars): | |
if not vars: | |
vars = {k: v for k,v in globals().items() if not k.startswith("_")} | |
self.__dict__.update(vars) | |
# usage - pulls in all global-level variables | |
vars = SPMHelper() | |
vars.some_variable | |
# usage - load local variables | |
local_vars = SPMHelper(**locals()) | |
# usage - load specific variables | |
my_vars = SPMHelper(my_var=my_var, another_var=another_var) | |
my_vars.another_var |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment