Re: The ternaery operator

2017-03-16 Thread Gregory Ewing

Stefan Ram wrote:

  A postponed »if« can be found (x if a, otherwise y), but a
  preceding »if« (if a, x, otherwise y) also is found often.


Yes, both forms occur in English. The point is that
Guido didn't just make the idea up, it was inspired
by natural language.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Chris Angelico
On Fri, Mar 17, 2017 at 10:45 AM, Stefan Ram  wrote:
> Gregory Ewing  writes:
>>wear(red_shirt if it_is_tuesday else green_shirt)
>
>   Operators in Python usually are written with special
>   symbols, not with words, so a word usually indicates,
>   "This is not an operator.".
>

Non-operators like "is [not]", "[not] in", "and", "or", and so on?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Gregory Ewing

Tobiah wrote:

To be fair, the proper comparison would be:

If it's Tuesday, I wear my red shirt, else my green one.


The Python analog of that would be

   if it_is_tuesday:
  wear(red_shirt)
   else:
  wear(green_shirt)

i.e. a statement rather than an expression. We're looking
for an English analog of a conditional expression:

   wear(red_shirt if it_is_tuesday else green_shirt)

For that, we need a conditional noun phrase, and the
only way to get one of those in English without sounding
like a non-native speaker is to put the condition in
the middle.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Jussi Piitulainen
William Mayor writes:

>> I think it would be nice to have a way of getting the 'true' value as
>> the return with an optional value if false.  The desire comes about
>> when the thing I'm comparing is an element of a collection:
>> 
>>drugs['choice'] if drugs['choice'] else 'pot'
>> 
>> Then I'm tempted to do:
>> 
>>  chosen = drugs['choice']
>>  chosen if chosen else 'pot'
>> 
>> I sometimes feel like doing:
>> 
>>  drugs['choice'] else 'pot'
>> 
>
> For the case where the element in the collection exists, but might be
> falsey you could do:
>
>   drugs[‘choice’] or ‘pot'

drugs.get('choice') or 'pot'

drugs.get('choice', 'pot')

[snip]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread alister
On Thu, 16 Mar 2017 03:12:32 +, Stefan Ram wrote:

> The syntax
> 
> a if c else b
> 
>   looks as if Guido made it intentionally ugly so that it will not be
>   used?
> 
>   Being able to detect patterns that are in widespread use among
>   programming languages enhances readability.
> 
>   IIRC, in Algol, there was an expression
> 
> if c then a else b
> 
>   . IIRC, most popular languages today use
> 
> c ? a : b
> 
>   . But now Guido has invented something totally new. Why?
> 
>   Perl has something like it, the "expression modifier".
>   Did Guido like /this/ so much, that he wanted to imitate it?

>>>import this

i think you will find at least 1 line that explains everything.




-- 
We were so poor that we thought new clothes meant someone had died.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread William Mayor
> 
> I think it would be nice to have a way of getting the 'true'
> value as the return with an optional value if false.  The desire
> comes about when the thing I'm comparing is an element of a collection:
> 
>drugs['choice'] if drugs['choice'] else 'pot'
> 
> Then I'm tempted to do:
> 
>   chosen = drugs['choice']
>   chosen if chosen else 'pot'
> 
> I sometimes feel like doing:
> 
>   drugs['choice'] else 'pot'
> 

For the case where the element in the collection exists, but might be falsey 
you could do:

drugs[‘choice’] or ‘pot'

The ternary operator would be useful for something like:

drugs[‘choice’] if is_good_for_you(drugs[‘choice’]) else ‘nice cup of 
tea’

Most of the time I avoid the ternary stuff though, I don’t think it’s easy to 
read, no matter what language you’re writing in.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Tobiah
On 03/16/2017 01:12 AM, Gregory Ewing wrote:
> Stefan Ram wrote:
> 
>> a if c else b
>>
>>   Guido has invented something totally new. Why?
> 
> It's arguably closer to the way you would say such a
> thing in English.
> 
> Consider the following sentences:
> 
> "I wear my red shirt if it's Tuesday, else my green one."
> 
> "I wear if it's Tuesday my red shirt, else my green one."
> 
> Which one sounds more natural?
> 

Actually, I see where now you are coming from:

  I wear (if a then b else c)

as opposed to

  I wear (b if a else c)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Tobiah
On 03/16/2017 01:12 AM, Gregory Ewing wrote:
> Stefan Ram wrote:
> 
>> a if c else b
>>
>>   Guido has invented something totally new. Why?
> 
> It's arguably closer to the way you would say such a
> thing in English.
> 
> Consider the following sentences:
> 
> "I wear my red shirt if it's Tuesday, else my green one."
> 
> "I wear if it's Tuesday my red shirt, else my green one."
> 
> Which one sounds more natural?
> 

To be fair, the proper comparison would be:

If it's Tuesday, I wear my red shirt, else my green one.



I think it would be nice to have a way of getting the 'true'
value as the return with an optional value if false.  The desire
comes about when the thing I'm comparing is an element of a collection:

drugs['choice'] if drugs['choice'] else 'pot'

Then I'm tempted to do:

chosen = drugs['choice']
chosen if chosen else 'pot'

I sometimes feel like doing:

drugs['choice'] else 'pot'


Tobiah


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Gregory Ewing

Stefan Ram wrote:


a if c else b

  Guido has invented something totally new. Why?


It's arguably closer to the way you would say such a
thing in English.

Consider the following sentences:

"I wear my red shirt if it's Tuesday, else my green one."

"I wear if it's Tuesday my red shirt, else my green one."

Which one sounds more natural?

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: The ternaery operator

2017-03-16 Thread Christian Gollwitzer

Am 16.03.17 um 04:12 schrieb Stefan Ram:

  The syntax

a if c else b

  looks as if Guido made it intentionally ugly so that it will
  not be used?
 [...]



  . But now Guido has invented something totally new. Why?


The rationale can be read in PEP 308:
https://www.python.org/dev/peps/pep-0308/

This should be the first place to go if you want to learn about Python 
language decisions. Asking here will just start the flame fire again.


Christian
--
https://mail.python.org/mailman/listinfo/python-list