Skip to content

Trinary operator in python

Friday, 15 April 2005  |  rich

Here's an example of an easy and compact way to simuate the trinary operator in python. It is not short circuit like the one in C, but it is pretty simple:

  newowner    = [self.newowner, None][self.contactid == self.newowner]

This will set newowner to self.newowner if self.contactid is not the same as self.newowner. Otherwise it will set it to None. [EDIT] How the hell do I stop drupal destroying square brackets? [EDIT] Aha, you need to use escape codes.