Python 2.5 one line if's

Before Python 2.5:
last_offset = MetaData().getLastOffset(uid)
offset = ''
if last_offset:
    offset = 'id > %i' % last_offset

After Python 2.5:

last_offset = MetaData().getLastOffset(uid)
offset = 'id > %i' % last_offset if last_offset else ''

Really nice syntactic sugar that makes code more readable.

17. Dec 2007 Code · Code improvement · Python · Tips
© Amir Salihefendic