Skip to content

Instantly share code, notes, and snippets.

@anthony-chaudhary
Last active November 13, 2019 20:59
Show Gist options
  • Save anthony-chaudhary/2494bc482902fada2248698f7b8af61e to your computer and use it in GitHub Desktop.
Save anthony-chaudhary/2494bc482902fada2248698f7b8af61e to your computer and use it in GitHub Desktop.
Defaults Proof of Concept (Rough)
class Test_Defaults:
def default_factory(self, x=None, type=None):
if x is None:
return self.default_by_type(type)
else:
return x
def default_by_type(self, type):
strategy_operations = {
"list" : list,
"dict" : dict,
"set" : set,
}
operation = strategy_operations.get(type.__class__.__name__)
if operation is None:
raise ValueError
return operation()
def __init__(self):
self.x = self.default_factory(None, [])
a = Test_Defaults()
b = Test_Defaults()
a.x.append(1)
print(a.x == b.x) # False as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment