python - Django: "referenced before assignment" but only for some variables -
i'm writing small app in django , i'm keeping state saved in few variables declare out of methods in views.py. here important part of file:
from app.playerlist import fulllist auc_unsold = fulllist[:] auc_teams = [] auc_in_progress = [] auc_current_turn = -1 print(auc_in_progress) def auc_action(request): data = json.loads(request.get["data"]) # ... elif data[0] == "start": random.shuffle(auc_teams) print(auc_unsold) print(auc_in_progress) auc_in_progress = [none, 0, none] print(auc_in_progress)
the auc_unsold
, auc_teams
variables work fine; auc_in_progress
variable not seen method, though, giving error in title. if take out print statement , let code assign value it, exception thrown somewhere else in code use variable again.
i have tried making variable , new 1 seems suffer problem well.
what happening?
edit: found solution: if write global auc_in_progress
before print statements, works fine. if try writing declare variable above doesn't work, though, reason.
i unsatisfied this, because don't know why happens , because dislike using global that, eh. has explanation?
you should absolutely not doing this, either original code or proposed solution global
.
anything @ module level shared across requests, not current user all users process. see same auction, etc.
the reason error because assign variable within function, automatically makes local variable: see this question more details. solution recommended there, same workaround - ie use global
- not appropriate here; should store data somewhere associated user, eg session.
Comments
Post a Comment