3

I am setting up an existing django project on a dreamhost web server, so far I have got everything to work correctly. However I developed under python 2.5 and dreamhost by default uses python 2.4. The following line seems gives a syntax error because of the if keyword:

'parent': c.parent.pk if c.parent is not None else None
                       ^

Is it the case that this form of if statement was introduced in Python 2.5, if so is there an easy change that would make it compatible with Python 2.4?

Or, should I just change to Python 2.5. I have already installed python 2.5 to a directory under my home directory, and have succeeded in running the python interpreter under 2.5. If I wish to use Python 2.5 for everything, where can I set this?

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
  • I just found this page http://www.siafoo.net/article/52#selecting-values which explains that this was in fact introduced in Python 2.5, however my second questions still stands. – Marcus Whybrow May 02 '10 at 13:56

2 Answers2

4

Yes, this kind of inline if was added with 2.5, released almost 4 years ago. You can update your Dreamhost version like this

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
2

http://www.diveintopython.net/power_of_introspection/and_or.html

(1 and [a] or [b])[0]
'parent': (c.parent is not None and [c.parent.pk] else [None])[0]
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
mg.
  • 7,822
  • 1
  • 26
  • 30
  • can be shortened to "c.parent or None" however that is slightly behavior changing as it is now doing a bool test, not a not None test – Gordon Wrigley Jan 17 '13 at 10:44