Pythonic code rewrite

After some years of Python programming I am getting the Pythonic touch. Here is a revelation!

One year ago or so I had written this:

if link:
  ns['LINK_START'] = '<a href="%s">' % (link)
  ns['LINK_END'] = '</a>'
else:
  ns['LINK_START'] = ''
  ns['LINK_END'] = ''

Today I would had written it like this:

ns['LINK_START'] = link and '<a href="%s">' % (link) or ''
ns['LINK_END'] = link and '</a>' or ''

Simple, readable and beautiful!

Code · Code improvement · Code rewrite · Python 9. Aug 2006
© Amir Salihefendic. Powered by Skeletonz.