sholden at holdenweb.com
Wed Jul 7 16:08:53 CEST 2004
Vincent Wehren wrote:
> "Thomas Lindgaard" <thomas at it-snedkeren.BLACK_HOLE.dk> schrieb im
> 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.
I should have thought that this is about the same as
self.foo = foo or someDefaultMutable
self.bar = bar or someOTherDefaultMutable
but as you say, tastes vary. I find the latter much more readable, as I
don't have to mentally convert the Boolean to a zero or one subscript
value to realise what happens when foo *is* None.
More information about the Python-list