如何避免写请求.GET.get()两次才能打印? [英] How to avoid writing request.GET.get() twice in order to print it?
本文介绍了如何避免写请求.GET.get()两次才能打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,您可以用一只石头杀死2只鸟:
而不是:
if(getData()){
$ data = getData();
echo $ data;
}
我可以这样做:
$ pre>
if($ data = getData()){
echo $ data;
}
您检查看看 getData()
存在,如果是,则将其分配给一个语句中的变量。
我想知道Python中有没有办法?所以,而不是这样做:
if request.GET.get('q'):
q = request.GET .get('q')
打印q
避免写 request.GET.get('q')
两次。
解决方案
可能不完全是你思考,但... ...
q = request.GET.get('q')
如果q:
打印q
这个?
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.
解决方案
Probably not exactly what you were thinking, but...
q = request.GET.get('q')
if q:
print q
this?
这篇关于如何避免写请求.GET.get()两次才能打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文