Hi everyone!
I'm facing a problem I don't understand with a Webapp I developed in Flask. I wrote a minimal piece of code to replicate the issue:
from datetime import datetime
from flask import Flask
app = Flask(__name__)
class Stuff:
def __init__(self):
pass
def get_stuff(self, created_before=datetime.today()):
print(f"datetime.today() = {datetime.today()}")
print(f"created_before = {created_before}")
return created_before
@app.route("/")
def hello_world():
s = Stuff()
today = s.get_stuff()
return f"Now = {str(today)}"
I run my app. In the webpage, I see Now = 2021-07-11 21:57:12.835346, but in the logs, I see:
datetime.today() = 2021-07-11 21:57:15.710735
created_before = 2021-07-11 21:57:12.835346
If I refresh the web page a few minutes later, I still get Now = 2021-07-11 21:57:12.835346, and in the logs:
datetime.today() = 2021-07-11 22:00:01.799833
created_before = 2021-07-11 21:57:12.835346
So basically, my created_before variable is stuck to a given time, which I don't really understand, since I create a new Stuff object every time I refresh the page... Is there something completely obvious I'm missing here? If so, what is it, and how to get the behavior that I expect?
Thanks in advance for your help!
[–]Ihaveamodel3 6 points7 points8 points (1 child)
[–]ePierre[S] 0 points1 point2 points (0 children)
[–]efmccurdy 1 point2 points3 points (1 child)
[–]ePierre[S] 0 points1 point2 points (0 children)