Garbage collection not working? Data seems to persist across browser requests with Flask/Gunicorn/Python.
If I make successive requests to a webserver hosting the code below, the output grows - it starts with ["test"] and next ["test", "test"] and so on. Can anyone explain the way Python garbage collection allows this? I would expect each request to the webserver to create a new instance of class Bad
and each new instance to start of with example
as an empty list.
@app.route('/bad')
def bad():
b = Bad()
b.append("test")
return b.output()
class Bad:
example = []
def append(self, data):
self.example.append(data)
def output(self):
return str(self.example)
I'm new to Python coming from PHP where the behaviour would be a single item array returned for every request to the webserver. I realise I can avoid the problem by using:
def __init__(self)
self.example = []
But I would like to properly understand what's happening.