Last active
September 3, 2021 14:11
-
-
Save mtovmassian/aa398f80d1d889b4bb849c3405c6b598 to your computer and use it in GitHub Desktop.
Anonymous class in Python
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
# https://stackoverflow.com/questions/1123000/does-python-have-anonymous-classes | |
anonymous_class = type( | |
"", | |
(), | |
{ | |
"attr1": None, | |
"get_attr1": lambda self: getattr(self, "attr1"), | |
"set_attr1": lambda self, value: setattr(self, "attr1", value) | |
} | |
) | |
ac = anonymous_class() | |
print(ac.get_attr1()) | |
ac.set_attr1("hello") | |
print(ac.get_attr1()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment