Short if

  • Thread starter Thomas Lindgaard
  • Start date
R

Reinhold Birkenfeld

Peter said:
Actually, it's nothing like that at all. I wrote about
the fact (as I see it) that further discussion will not
result in a change to the situation. I said nothing about
me or anyone else (assuming I'm in the group you refer to
as "us adults") paying attention to those who want to
discuss it further. If enough motivated people could
reach a consensus, it's possible Guido would actually relent
and put the strong majority's ternary in someday. Only
Tim can say. :)

I used a wink, and you mentioned "friendly", so I'll assume
this should all be light-hearted and avoid taking offense that
you would try to put such condescending words in my mouth.

Yes, that's right. It's the spirit of the Python community, I think, and
this is a Good Thing[tm].

Reinhold
 
D

David Bolen

Peter Hansen said:
That is true of course, but keep in mind also that the most common
use of such patterns is actually to initialize self.foo to an
*empty* mutable of the appropriate type. In that case (and that
case alone), it is generally equivalent at least in outcome, as
if the user passes in an empty (say, list), the above code will
use the "someDefaultMutable" empty list instead, but the result
is still what was desired in most cases.

Maybe, maybe not. Using an alternate empty list is only equivalent if
the function is not supposed to mutate the supplied parameter, but is
instead, for example, returning a reference to the resultant mutable
object. (The original sample didn't include the full function so I
don't know if it was returning the reference).

Given that a mutable object is coming in as a parameter, and an
optional parameter at that, I think it's probably even odds that if it
was supplied, the function may be expected to mutate the object that
the caller supplied.
But it is good to know the distinctions, and the fact that there
are such distinctions still means that good ol' "if/else" is still
the safest way to go...

Yep.

-- David
 
C

Chris S.

Paul said:
Does Python have a short if like C or PHP:

bool = false
string = 'This is ' + (( bool ) ? 'true' : 'false')



If you are really convinced that this is a good road to go down (think of
code readability), you could do

bool = False # check capitalisation!
string = 'This is ' + ('false','true')[bool]

I never liked the C ? operator, always finding code was clearer using the
full if, and this hack is even worse!

It's not about code readability. It's about functional programming.
Lambda expressions don't make very readable code, but Python supports
them anyway. If Python is serious about it's functional aspects, it
requires a ternary operator. Only in the context of functional
programming is this relevant, which makes the whole "it's ugly" argument
moot. Besides, a real ternary, instead of
((condition) and [truecase] or [falsecase])[0]
looks a whole lot better IMO.
 
P

Paul Rubin

Paul Sweeney said:
bool = false
string = 'This is ' + (( bool ) ? 'true' : 'false')

If you are really convinced that this is a good road to go down (think of
code readability), you could do

bool = False # check capitalisation!
string = 'This is ' + ('false','true')[bool]

But that can easily become incorrect when the branches have side effects:

string = 'This is ' + (false(), true())[bool]

You have to do something like:

string = 'This is ' + (lambda: false(), lambda: true())[bool]()
I never liked the C ? operator, always finding code was clearer using the
full if, and this hack is even worse!

Which is more readable depends on the context. Sometimes the 'if'
just bloats up your code.
 
A

Alex Hunsley

Thomas said:
Hello

Does Python have a short if like C or PHP:

bool = false
string = 'This is ' + (( bool ) ? 'true' : 'false')

?

No, thankfully! It's ugly and makes for less maintainable code IMO (as it is
effectively an inside-out if that you have to rewrite if you want to add any
more conditional code).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top