Session.pop方法的第二个参数在Python Flask中有什么作用? [英] What does the second argument of the Session.pop method do in Python Flask?

查看:65
本文介绍了Session.pop方法的第二个参数在Python Flask中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Flask教程,只是想确切说明会话对象的.pop属性是做什么的,以及为什么它将采用无"参数.

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'))

推荐答案

根据 Flask的API 他们的 Session 类是python Dict 的包装.根据 Python文档中的 dict.pop():

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 .如果未提供 default ,并且字典中没有 key ,则会引发 KeyError .

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.

在这种情况下,本教程要求您传入 None 作为默认值.

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

这篇关于Session.pop方法的第二个参数在Python Flask中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆