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

前端 未结 10 766
名媛妹妹
名媛妹妹 2020-12-04 21:34

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

10条回答
  •  青春惊慌失措
    2020-12-04 22:30

    a possible way to do it, without necessity to set the variable before, could be like:

    if (lambda x: globals().update({'q':x}) or True if x else False)(request.GET.get('q')):
        print q
    

    .. it's just for fun - this method should not be used, because it is ugly hack, difficult to understand at first sight, and it creates/overwrites a global variable (only if the condition is met, though)

提交回复
热议问题