Created
October 28, 2022 01:06
-
-
Save amitbhadra/d59f4f85e3e89698240b76844d5b5881 to your computer and use it in GitHub Desktop.
lazy evaluation
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
$ python test1.py | |
{'id': 3} | |
{'id': 3} | |
$ python test2.py | |
{'id': 2} | |
{'id': 3} | |
why different?? |
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
module_var = { | |
'id': None | |
} |
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
from module import module_var | |
class child(object): | |
def __init__(self, var=None): | |
self.child_var = module_var | |
if var: | |
self.child_var['id'] = var | |
def update(self, new_var): | |
self.child_var['id'] = new_var | |
def get(self): | |
return self.child_var | |
class parent(object): | |
def __init__(self): | |
self.child_records = {} | |
def new(self, name, var): | |
record = child(var) | |
self.child_records[name] = record | |
def get(self, name): | |
return self.child_records.get(name) | |
p = parent() | |
p.new('A', 2) | |
p.new('B', 3) | |
print(p.get('A').get()) | |
print(p.get('B').get()) |
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
from module import module_var | |
class child(object): | |
def __init__(self, var=None): | |
#self.child_var = module_var | |
self.child_var = { | |
'id': None | |
} | |
if var: | |
self.child_var['id'] = var | |
def update(self, new_var): | |
self.child_var['id'] = new_var | |
def get(self): | |
return self.child_var | |
class parent(object): | |
def __init__(self): | |
self.child_records = {} | |
def new(self, name, var): | |
record = child(var) | |
self.child_records[name] = record | |
def get(self, name): | |
return self.child_records.get(name) | |
p = parent() | |
p.new('A', 2) | |
p.new('B', 3) | |
print(p.get('A').get()) | |
print(p.get('B').get()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment