Created
January 12, 2024 20:14
-
-
Save galenseilis/2a8ed5e3edb8ad66855629504a217c2c to your computer and use it in GitHub Desktop.
dictionary_to_object.py
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
import types | |
class DictToObject: | |
def __init__(self, dictionary): | |
for key, value in dictionary.items(): | |
if callable(value): | |
# If the value is callable (a function/method), bind it to the instance | |
setattr(self, key, types.MethodType(value, self)) | |
else: | |
# Otherwise, set it as an attribute | |
setattr(self, key, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment