How to avoid writing request.GET.get() twice in order to print it?

dictionary, if-statement, python

Solution

Probably not exactly what you were thinking, but...

q = request.GET.get('q')
if q:
    print q

this?

Problem

I come from a PHP background and would like to know if there's a way to do this in Python. In PHP you can kill 2 birds with one stone like this: Instead of: ``` if(getData()){ $data = getData(); echo $data; } ``` I can do this: ``` if($data = getData()){ echo $data; } ``` You check to see if `getData()` exists AND if it does, you assign it to a variable in one statement. I wanted to know if there's a way to do this in Python? So instead of doing this: ``` if request.GET.get('q'): q = request.GET.get('q') print q ``` avoid writing `request.GET.get('q')` twice.

Original source

Related problems