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

Question:

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.

Asked By: givp

||

Answers:

Probably not exactly what you were thinking, but…

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

this?

Answered By: Amber

See my 8-year-old recipe here for just this task.

# In Python, you can't code "if x=foo():" -- assignment is a statement, thus
# you can't fit it into an expression, as needed for conditions of if and
# while statements, &c.  No problem, if you just structure your code around
# this.  But sometimes you're transliterating C, or Perl, or ..., and you'd
# like your transliteration to be structurally close to the original.
#
# No problem, again!  One tiny, simple utility class makes it easy...:

class DataHolder:
    def __init__(self, value=None): self.value = value
    def set(self, value): self.value = value; return value
    def get(self): return self.value
# optional but handy, if you use this a lot, either or both of:
setattr(__builtins__,'DataHolder',DataHolder)
setattr(__builtins__,'data',DataHolder())

# and now, assign-and-set to your heart's content: rather than Pythonic
while 1:
    line = file.readline()
    if not line: break
    process(line)
# or better in modern Python, but quite far from C-like idioms:
for line in file.xreadlines():
    process(line)
# you CAN have your C-like code-structure intact in transliteration:
while data.set(file.readline()):
    process(data.get())
Answered By: Alex Martelli

Well, this would be one way

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

A briefer (but not superior, due to the call to print of nothing) way would be

print request.GET.get('q') or '',
Answered By: Grumdrig
q = request.GET.get('q')
if q:
    print q
else:
    # q is None
    ...

There’s no way of doing assignment and conditionals in one go…

Answered By: Adam

If get() throws an exception when it’s not there, you could do

try:
   q = request.GET.get('q')
   print q
except :
   pass
Answered By: Rizwan Kassim

A variation on Alex’s answer:

class DataHolder:
    def __init__(self, value=None, attr_name='value'):
        self._attr_name = attr_name
        self.set(value)
    def __call__(self, value):
        return self.set(value)
    def set(self, value):
        setattr(self, self._attr_name, value)
        return value
    def get(self):
        return getattr(self, self._attr_name)
save_data = DataHolder()

Usage:

if save_data(get_input()):
    print save_data.value

or if you prefer an alternative interface:

if save_data.set(get_input()):
    print save_data.get()

I would find this helpful to test a series of regular expressions in an if-elif-elif-elif etc construct, as in this SO question:

import re

input = u'test bar 123'
save_match = DataHolder(attr_name='match')
if save_match(re.search('foo (d+)', input)):
    print "Foo"
    print save_match.match.group(1)
elif save_match(re.search('bar (d+)', input)):
    print "Bar"
    print save_match.match.group(1)
elif save_match(re.search('baz (d+)', input)):
    print "Baz"
    print save_match.match.group(1)
Answered By: Craig McQueen
config_hash = {}
tmp_dir = ([config_hash[x]  for x in ["tmp_dir"] if config_hash.has_key(x)] or ["tmp"])[0]
print tmp_dir
config_hash["tmp_dir"] = "cat"
tmp_dir = ([config_hash[x]  for x in ["tmp_dir"] if config_hash.has_key(x)] or ["tmp"])[0]
print tmp_dir
Answered By: louis_xv

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)

Answered By: mykhal

Simply try:

print(request.GET.get('q', ''))

which basically prints nothing if the first argument is not present (see dict.get).


Alternative solution would be to use a conditional expression in Python:

<expression1> if <condition> else <expression2>

but you’ll end up repeating variable twice, for example:

print(request.GET.get('q') if request.GET.get('q') else '')

For variable assignments in loops, check in here.

Answered By: kenorb

PEP 572 introduces Assignment Expressions. From Python 3.8 and onwards you can write:

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

Here are some more examples from the Syntax and semantics part of the PEP:

# Handle a matched regex
if (match := pattern.search(data)) is not None:
    # Do something with match

# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
   process(chunk)

# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]
Answered By: timgeb
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.