Short if

Vincent Wehren vwehren at home.nl
Wed Jul 7 09:08:38 EDT 2004


"Thomas Lindgaard" <thomas at it-snedkeren.BLACK_HOLE.dk> schrieb im
Newsbeitrag
news:pan.2004.07.07.11.13.46.804417 at it-snedkeren.BLACK_HOLE.dk...
| On Wed, 07 Jul 2004 11:14:45 +0200, Thomas Lindgaard wrote:
|
| > Hello
| >
| > Does Python have a short if like C or PHP:
| >
| >   bool = false
| >   string = 'This is ' + (( bool ) ? 'true' : 'false')
| >
| > ?
|
| Ok, I see that this is not really the language for short ifs (writing
| ('true', 'false')[bool] does not strike me as being very readable, so I
| think I'll revert to the long version).

The readability concern is justified. Still, I find myself using this style
a lot, especially in situations such as:

class Klass:
  def __init__(self, a, b, foo=None, bar=None):

      self.a = a
      self.b = b
      self.foo = (foo, someDefaultMutable)[foo is None]
      self.bar = (bar, someOtherDefaultMutable)[bar is None]


I don't like putting a lot of else/if blocks in __init__, but that's just
personal taste I guess.

--
Vincent Wehren








|
| Used carefully, though, I think that the ternary operator is very useful.



|
| -- 
| Regards
| /Thomas
|





More information about the Python-list mailing list