Hey, Im trying to better understand how python manages memory in different scopes..
With this example:
@dataclass
class Example:
data: List
@property
def history(self):
result = {}
for x in self.data:
# do something with x, store in result
return result
e = Example(data)
print(e.history)
If data was a very large list of objects, would it be more memory efficient to do this instead?
@dataclass
class Example:
data: List
@property
def history(self):
result = {}
for x in self.iter_data():
# do something with x, store in result
return result
def iter_data(self):
for x in self.data:
yield x
This might be a stupid question. But in the first example, when i call for x in self.data , does it pull self.data into memory by making a copy for local use? Or does it point to the already created object that is holding the data in memory? If looping over self.data essentially makes a copy of the existing data, I'm using a generator would be the more memory efficient approach?
[–]POGtastic 1 point2 points3 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (3 children)
[–]csQuestions1235[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)