如何避免写入 request.GET.get() 两次以打印它? [英] How to avoid writing request.GET.get() twice in order to print it?

查看:26
本文介绍了如何避免写入 request.GET.get() 两次以打印它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 PHP 背景,想知道是否有办法在 Python 中执行此操作.

I come from a PHP background and would like to know if there's a way to do this in Python.

在 PHP 中,你可以像这样用一颗石头杀死 2 只鸟:

In PHP you can kill 2 birds with one stone like this:

代替:

if(getData()){
    $data = getData();
    echo $data;
}

我可以做到:

if($data = getData()){
    echo $data;
}

您检查 getData() 是否存在,如果存在,则在一个语句中将其分配给一个变量.

You check to see if getData() exists AND if it does, you assign it to a variable in one statement.

我想知道是否有办法在 Python 中做到这一点?所以不要这样做:

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

避免将 request.GET.get('q') 写两次.

推荐答案

可能不是你想的那样,但是...

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

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

这个?

这篇关于如何避免写入 request.GET.get() 两次以打印它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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