Using python 2.7, I have the following code:
if result != None:
(data,) = result
return data
return None
The result variable is returned from a sqlite3 query that returns only one value. If the query returns a result, I want to unpack and return the data, otherwise I want to return None. The code above seems overly verbose and not at all pythonic. Is there a better way?
You could use a
ifelsecondition:or simplify this down to:
if you don’t care about
resultpossibly being some other false-y value such as an empty tuple and such.