Skip to content

Instantly share code, notes, and snippets.

@ncopiy
Created June 26, 2019 16:30
Show Gist options
  • Save ncopiy/bb2a76b5d7295158b5347631dc98ad84 to your computer and use it in GitHub Desktop.
Save ncopiy/bb2a76b5d7295158b5347631dc98ad84 to your computer and use it in GitHub Desktop.
defaultattr (like defaultdict but for attributes)
class defaultattr:
def __init__(self, expected_type):
self.expected_type = expected_type
def __getattr__(self, expected_type):
if isinstance(self.expected_type, type):
return self.expected_type()
return self.expected_type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment