Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the second argument of the Session.pop method do in Python Flask?

I'm working through the Flask tutorial and would just like to clarify exactly what the .pop attr of the session object does and why it would take a 'None' parameter.

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('show_entries'))
like image 653
Takeshi Patterson Avatar asked Nov 21 '13 08:11

Takeshi Patterson


People also ask

How do I pop a Flask session?

To release a session variable use pop() method. The following code is a simple demonstration of session works in Flask. URL '/' simply prompts user to log in, as session variable 'username' is not set. As user browses to '/login' the login() view function, because it is called through GET method, opens up a login form.

How does session in Flask work?

Flask signs the data with the app's secret key when sending it, and unsigns it with the same key when reading it. Flask does not add anything to the session. There is no session id, the browser just sends the session cookie during each request, and Flask reads it.

What is the session object in Flask?

In the flask, a session object is used to track the session data which is a dictionary object that contains a key-value pair of the session variables and their associated values. ADVERTISEMENT. ADVERTISEMENT. The following syntax is used to set the session variable to a specific value on the server.

How many sessions are in a Flask?

In March 2022, The CW renewed the series for a ninth and final season. As of June 29, 2022, 171 episodes of The Flash have aired, concluding the eighth season.


1 Answers

According to Flask's API their Session class is a wrapper around a python Dict. According to the python documentation for dict.pop():

pop(key[, default])

If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

In this case the tutorial asks you to pass in None as the default value.

like image 171
Vikram Saran Avatar answered Oct 27 '22 16:10

Vikram Saran