Skip to content

Instantly share code, notes, and snippets.

@galenseilis
Created January 12, 2024 20:14
Show Gist options
  • Save galenseilis/2a8ed5e3edb8ad66855629504a217c2c to your computer and use it in GitHub Desktop.
Save galenseilis/2a8ed5e3edb8ad66855629504a217c2c to your computer and use it in GitHub Desktop.
dictionary_to_object.py
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