I've run into an odd issue. It started with trying to create objects in django in a loop. I have an override on the model save() that does something like
self.history.append(new_status)
Now, I thought self meant the current instance being evaluated. But, when run in a loop (on a single python process) self.history caches from the last time it was evaluated.
So, in the example:
for i in range(3):
job = Job(...properties, exluding history, as that's done in the save() method)
job.save()
The jobs stored in the database look like this:
job1 - id: 1, history: ['pending']
job2 - id: 2, history: ['pending', 'pending']
job3 - id: 3, history: ['pending', 'pending', 'pending']
... so on
Now, if the django server is not restarted, and this controller is invoked again, then the jobs will be created, starting with history of the last object it created (so it would start with 4 pendings, then the next 5 and so on).
Essentially, Previous X + current N records
I have been able to largely replicate this in simple Python:
class Base(object):
id = None
name = None
history = list()
def __init__(self, id=None, name=None):
self.id = id
self.name = name
def save(self, **kwargs):
for arg in kwargs:
self.__setattr__(arg, kwargs.get(arg))
class Queue(Base):
def save(self, **kwargs):
self.history.append({'status': 'test'})
super(Queue, self).save()
items = []
for i in range(5):
items.append(Queue(
id=i,
name=str(i) + '-test'
))
for _ in items:
_.save()
print(_.history)
when run in a debugger, you get this:
[{'status': 'test'}]
[{'status': 'test'}, {'status': 'test'}]
[{'status': 'test'}, {'status': 'test'}, {'status': 'test'}]
[{'status': 'test'}, {'status': 'test'}, {'status': 'test'}, {'status': 'test'}]
[{'status': 'test'}, {'status': 'test'}, {'status': 'test'}, {'status': 'test'}, {'status': 'test'}]
I really can't figure this out - it smells like this can't be a python issue, but a lack of understanding of how objects/referencing works in Python.
Any help would be great! Thanks.