python - How 'with' statement works in Flask (jinja2)? -


in pure python code use with statement (source):

class controlled_execution:     def __enter__(self):         # set things         return thing     def __exit__(self, type, value, traceback):         # tear things down  controlled_execution() thing:      # code 

in flask/jinja2 standard procedure using flash messages following (source):

{% messages = get_flashed_messages() %}   {% if messages %}     {% message in messages %}       <!-- stuff `message` -->     {% endfor %}           {% endif %} {% endwith %} 

i'd know how {% messages = get_flashed_messages() %} works in terms of syntax.

i failed recreate in pure python:

  • with messages = get_flashed_messages(): pass raises syntaxerror
  • with get_flashed_messages() messages: pass raises attributeerror: __exit__

(i've imported get_flashed_messages flask in both cases).

the with statement in flask not same with statement in python.

within python equivalent this:

messages = get_flashed_messages() 

Popular posts from this blog