Last active
November 13, 2019 20:59
-
-
Save anthony-chaudhary/2494bc482902fada2248698f7b8af61e to your computer and use it in GitHub Desktop.
Defaults Proof of Concept (Rough)
This file contains hidden or 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
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