Skip to content

Instantly share code, notes, and snippets.

@mtovmassian
Last active September 3, 2021 14:11
Show Gist options
  • Save mtovmassian/aa398f80d1d889b4bb849c3405c6b598 to your computer and use it in GitHub Desktop.
Save mtovmassian/aa398f80d1d889b4bb849c3405c6b598 to your computer and use it in GitHub Desktop.
Anonymous class in Python
# 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