Newsbeitrag
| 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
|