librelist archives

« back to archive

local variable 'session' referenced before assignment

local variable 'session' referenced before assignment

From:
Paul Morrow
Date:
2010-12-15 @ 22:22
"""
The following works as expected.  But if you uncomment the
del(session) call, you'll get the exception

   local variable 'session' referenced before assignment

which is very strange as that branch of the if is never executed.
"""

from flask import Flask, session, request
app = Flask(__name__)

@app.route("/")
def hello():
    if request.method == 'GET':
        session['foo'] = 10
        return 'ok'
    elif request.method == 'NOT A VALID METHOD':
        #del(session)
        pass

if __name__ == "__main__":
    app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
    app.run(host='0.0.0.0', debug=True)