Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-12-01 Thread Steven D'Aprano
On Thu, Nov 30, 2017 at 08:02:08PM +0300, Ivan Pozdeev via Python-ideas wrote:

> My experience with these operators in C# says:
> * They do save "more than a few keystrokes". Even more importantly, they 
> allow to avoid double evaluation or the need for a temporary variable 
> workaround that are inherent in " if  else "
>     * (An alternative solution for the latter problem would be an 
> assignment expression, another regularly rejected proposal.)
> * They make it temptingly easy and implicit to ignore errors.

How? 

> * They are alien to Python's standard semantics on search failure which 
> is to raise an exception rather than return None

Alien, like this?

py> mo = re.match(r'\d:', 'abc')
py> mo is None
True


Besides, who said this is limited to searching? I don't remember even a 
single example in the PEP being about searching.



-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-30 Thread Chris Angelico
On Fri, Dec 1, 2017 at 5:45 AM, Tres Seaver  wrote:
>> I would *not* add any spelling for an explicit bare-except equivalent.
>> You would have to write:
>>
>>   val = name.strip()[4:].upper() except Exception as -1
>
>
> Wouldn't that really need to be this instead, for a true 'except:' 
> equivalence:
>
>   val = name.strip()[4:].upper() except BaseException as -1
>

Read the rejected PEP 463 for all the details and arguments. All this
has been gone into.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-30 Thread Brett Cannon
On Wed, 29 Nov 2017 at 13:28 Greg Ewing  wrote:

> Nick Coghlan wrote:
>
> >>What about more English-like syntax:
> >>
> >>X or else Y
> >
> > The problem with constructs like this is that they look like they
> > should mean the same thing as "X or Y".
>
> How about:
>
> x otherwise y
>
> It looks different enough from "or" that you're not going
> to accidentally read it that way when skimming.
>
> The meaning is something you'll have to learn if you don't
> know, but at least it's a word that can be googled for.
>

 In terms of syntax, this is my favourite one so far.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-30 Thread Ivan Pozdeev via Python-ideas

On 29.11.2017 9:08, Steven D'Aprano wrote:

On Tue, Nov 28, 2017 at 12:31:06PM -0800, Raymond Hettinger wrote:

I also cc python-dev to see if anybody here is strongly in favor or against 
this inclusion.

Put me down for a strong -1.  The proposal would occasionally save a
few keystokes but comes at the expense of giving Python a more Perlish
look and a more arcane feel.

I think that's an unfair characterisation of the benefits of the PEP.
It's not just "a few keystrokes".

Ironically, the equivalent in Perl is // which Python has used for
truncating division since version 2.4 or so. So if we're in danger of
looking "Perlish", that ship has sailed a long time ago.

Perl is hardly the only language with null-coalescing operators -- we
might better describe ?? as being familiar to C#, PHP, Swift and Dart.
That's two mature, well-known languages and two up-and-coming languages.

My experience with these operators in C# says:
* They do save "more than a few keystrokes". Even more importantly, they 
allow to avoid double evaluation or the need for a temporary variable 
workaround that are inherent in " if  else "
    * (An alternative solution for the latter problem would be an 
assignment expression, another regularly rejected proposal.)

* They make it temptingly easy and implicit to ignore errors.
* They are alien to Python's standard semantics on search failure which 
is to raise an exception rather than return None


[...]

 timeout ?? local_timeout ?? global_timeout

As opposed to the status quo:

 timeout if timeout is not None else (local_timeout if local_timeout is not 
None else global_timeout)

Or shorter, but even harder to understand:

 (global_timeout if local_timeout is None else local_timeout) if timeout is 
None else timeout

I'd much prefer to teach the version with ?? -- it has a simple
explanation: "the first of the three given values which isn't None". The
?? itself needs to be memorized, but that's no different from any other
operator. The first time I saw ** I was perplexed and couldn't imagine
what it meaned.

Here ?? doesn't merely save a few keystrokes, it significantly reduces
the length and complexity of the expression and entirely cuts out the
duplication of names.

If you can teach

 timeout or local_timeout or global_timeout

then you ought to be able to teach ??, as it is simpler: it only
compares to None, and avoids needing to explain or justify Python's
truthiness model.



 'foo' in (None ?? ['foo', 'bar'])

If you can understand

 'foo' in (False or ['foo', 'bar'])

then surely you can understand the version with ??.



 requested_quantity ?? default_quantity * price

Again:

 (default_quantity if requested_quantity is None else requested_quantity) * 
price


I'd much prefer to read, write and teach the version with ?? over the
status quo.




--
Regards,
Ivan

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread electronnn
I, too, think that this is too special a case for a dedicated syntax but so
far I prefer:

  x None-or y

The meaning is more clear to me. None-or is like regular or except that y
is evaluated only if x is None.

On Thu, Nov 30, 2017 at 2:35 AM, MRAB  wrote:

> On 2017-11-29 21:27, Greg Ewing wrote:
>
>> Nick Coghlan wrote:
>>
>> What about more English-like syntax:

 X or else Y

>>>
>>> The problem with constructs like this is that they look like they
>>> should mean the same thing as "X or Y".
>>>
>>
>> How about:
>>
>>  x otherwise y
>>
>> It looks different enough from "or" that you're not going
>> to accidentally read it that way when skimming.
>>
>> The meaning is something you'll have to learn if you don't
>> know, but at least it's a word that can be googled for.
>>
>> It has the disadvantage that it's quite long. How about "alt"
> (abbreviation for "alternatively"):
>
> x alt y
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread MRAB

On 2017-11-29 21:27, Greg Ewing wrote:

Nick Coghlan wrote:


What about more English-like syntax:

X or else Y


The problem with constructs like this is that they look like they
should mean the same thing as "X or Y".


How about:

 x otherwise y

It looks different enough from "or" that you're not going
to accidentally read it that way when skimming.

The meaning is something you'll have to learn if you don't
know, but at least it's a word that can be googled for.

It has the disadvantage that it's quite long. How about "alt" 
(abbreviation for "alternatively"):


x alt y
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Steven D'Aprano
On Wed, Nov 29, 2017 at 06:49:14AM -0800, David Mertz wrote:
> I didn't say no use case exists, but rather "too special case." And/or
> shortcutting is great, but truthiness feels much more general than noneness
> to me.

Of course truthiness is much more general than Noneness -- and that's 
precisely the problem with using `or`. It's too general.

If you haven't read the PEP, you should, because this is extensively 
discussed there. One of the problems the author discusses is that `or` 
is recommended on Stackoverflow as the best way to perform short-cutting 
None tests, often with no caveats given.

We've been here before. Python old-timers may remember the former 
idiomatic way to get a ternary if expression:

condition and first or second

which was also buggy (if first happens to be falsey, second is returned 
even when condition is true).



-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Chris Angelico
On Thu, Nov 30, 2017 at 8:56 AM, Stephan Houben  wrote:
>
>
> Op 29 nov. 2017 22:35 schreef "Greg Ewing" :
>
>
> It would read better with some kind of pronoun in there:
>
>A if it is not None else C
>
> Hypercard's Hypertalk had a special variable "it" that
> worked sort of like that.
>
>
> I  considered that,  but there are two issues.
>
> 1. Backward-incompatible change
>
> 2. The semantics of
> A if B else C
> now depends on if B contains 'it',
> in which case A gets evaluated unconditionally and prior to B.
> What if evaluation of 'it' is itself conditional, e.g.
>
> print("hello") if a or it else print("goodbye")
>
> Note that in the
>
> A if  B else C
>
> proposal the evaluation of the implicit 'it' is never conditional in that
> way.

The only way would be for "it" to be a keyword. You can't misinterpret
"A if pass is not None else C", or "A if def is not None else C", as
they're currently syntax errors. Obviously we don't want the word "it"
to become a keyword (massive breakage right there), and I'm pretty
sure there's no existing keyword that actually makes sense here, so
this proposal would depend on coming up with a useful word that
wouldn't collide too much. I'm -0.9 unless someone can find the
perfect word.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Stephan Houben
Op 29 nov. 2017 22:35 schreef "Greg Ewing" :


It would read better with some kind of pronoun in there:

   A if it is not None else C

Hypercard's Hypertalk had a special variable "it" that
worked sort of like that.


I  considered that,  but there are two issues.

1. Backward-incompatible change

2. The semantics of
A if B else C
now depends on if B contains 'it',
in which case A gets evaluated unconditionally and prior to B.
What if evaluation of 'it' is itself conditional, e.g.

print("hello") if a or it else print("goodbye")

Note that in the

A if  B else C

proposal the evaluation of the implicit 'it' is never conditional in that
way.

Stephan



-- 
Greg

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Greg Ewing

Stephan Houben wrote:


A if is not None else C


Reading that gives me the feeling that something in my
brain has slipped a tooth.

It would read better with some kind of pronoun in there:

   A if it is not None else C

Hypercard's Hypertalk had a special variable "it" that
worked sort of like that.

--
Greg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Greg Ewing

Stephan Houben wrote:


X or else Y


Sounds like a Python dialect for Mafia use.

   customer.repay(loan) or else apply(baseball_bat, customer.kneecaps)

--
Greg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Greg Ewing

Nick Coghlan wrote:


What about more English-like syntax:

X or else Y


The problem with constructs like this is that they look like they
should mean the same thing as "X or Y".


How about:

   x otherwise y

It looks different enough from "or" that you're not going
to accidentally read it that way when skimming.

The meaning is something you'll have to learn if you don't
know, but at least it's a word that can be googled for.

--
Greg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread David Mertz
True enough. I remember the prior discussion, but didn't look up the PEP
number. I know it's hard to revisit these ideas, but occasionally it works,
especially if a nicer spelling is found (Barry proposed some a bit
different than my ideas)

On Nov 29, 2017 9:55 AM, "Eric V. Smith"  wrote:


On Nov 29, 2017, at 12:40 PM, David Mertz  wrote:

I like much of the thinking in Random's approach. But I still think None
isn't quite special enough to warrant it's own syntax.

However, his '(or None: name.strip()[4:].upper())' makes me realize that
what is being asked in all the '?(', '?.', '?[' syntax ideas is a kind of
ternary expression.  Except the ternary isn't based on whether a predicate
holds, but rather on whether an exception occurs (AttributeError, KeyError,
TypeError).  And the fallback in the ternary is always None rather than
being general.

I think we could generalize this to get something both more Pythonic and
more flexible.  E.g.:

val = name.strip()[4:].upper() except None

This would just be catching all errors, which is perhaps too broad.  But it
*would* allow a fallback other than None:

val = name.strip()[4:].upper() except -1

I think some syntax could be possible to only "catch" some exceptions and
let others propagate.  Maybe:

val = name.strip()[4:].upper() except (AttributeError, KeyError): -1

I don't really like throwing a colon in an expression though.  Perhaps some
other word or symbol could work instead.  How does this read:

val = name.strip()[4:].upper() except -1 in (AttributeError, KeyError)

Where the 'in' clause at the end would be optional, and default to
'Exception'.

I'll note that what this idea DOES NOT get us is:

  val = timeout ?? local_timeout ?? global_timeout

Those values that are "possibly None" don't raise exceptions, so they
wouldn't apply to this syntax.


See the rejected PEP 463 for Exception catching expressions.

Eric.


Yours, David...


On Wed, Nov 29, 2017 at 9:03 AM, Random832  wrote:

> On Tue, Nov 28, 2017, at 15:31, Raymond Hettinger wrote:
> >
> > > I also cc python-dev to see if anybody here is strongly in favor or
> against this inclusion.
> >
> > Put me down for a strong -1.   The proposal would occasionally save a few
> > keystokes but comes at the expense of giving Python a more Perlish look
> > and a more arcane feel.
> >
> > One of the things I like about Python is that I can walk non-programmers
> > through the code and explain what it does.  The examples in PEP 505 look
> > like a step in the wrong direction.  They don't "look like Python" and
> > make me feel like I have to decrypt the code to figure-out what it does.
> >
> > timeout ?? local_timeout ?? global_timeout
> > 'foo' in (None ?? ['foo', 'bar'])
> > requested_quantity ?? default_quantity * price
> > name?.strip()[4:].upper()
> > user?.first_name.upper()
>
> Since we're looking at different syntax for the ?? operator, I have a
> suggestion for the ?. operator - and related ?[] and ?() that appeared
> in some of the proposals. How about this approach?
>
> Something like (or None: ...) as a syntax block in which any operation
> [lexically within the expression, not within e.g. called functions, so
> it's different from simply catching AttributeError etc, even if that
> could be limited to only catching when the operand is None] on None that
> is not valid for None will yield None instead.
>
> This isn't *entirely* equivalent, but offers finer control.
>
> v = name?.strip()[4:].upper() under the old proposal would be more or
> less equivalent to:
>
> v = name.strip()[4:].upper() if name is not None else None
>
> Whereas, you could get the same result with:
> (or None: name.strip()[4:].upper())
>
> Though that would technically be equivalent to these steps:
> v = name.strip if name is not None else None
> v = v() if v "
> v = v[4:] """
> v = v.upper """
> v = v() """
>
> The compiler could optimize this case since it knows none of the
> operations are valid on None. This has the advantage of being explicit
> about what scope the modified rules apply to, rather than simply
> implicitly being "to the end of the chain of dot/bracket/call operators"
>
> It could also be extended to apply, without any additional syntax, to
> binary operators (result is None if either operand is None) (or None: a
> + b), for example, could return None if either a or b is none.
>
> [I think I proposed this before with the syntax ?(...), the (or None:
> ...) is just an idea to make it look more like Python.]
> ___
> Python-Dev mailing list
> python-...@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/mertz%
> 40gnosis.cx
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from 

Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Barry Warsaw
On Nov 29, 2017, at 12:40, David Mertz  wrote:

> I think some syntax could be possible to only "catch" some exceptions and let 
> others propagate.  Maybe:
> 
>val = name.strip()[4:].upper() except (AttributeError, KeyError): -1
> 
> I don't really like throwing a colon in an expression though.  Perhaps some 
> other word or symbol could work instead.  How does this read:
> 
>val = name.strip()[4:].upper() except -1 in (AttributeError, KeyError)

I don’t know whether I like any of this  but I think a more natural 
spelling would be:

   val = name.strip()[4:].upper() except (AttributeError, KeyError) as -1

which could devolve into:

   val = name.strip()[4:].upper() except KeyError as -1

or:

   val = name.strip()[4:].upper() except KeyError # Implicit `as None`

I would *not* add any spelling for an explicit bare-except equivalent.  You 
would have to write:

   val = name.strip()[4:].upper() except Exception as -1

Cheers,
-Barry



signature.asc
Description: Message signed with OpenPGP
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Eric V. Smith

> On Nov 29, 2017, at 12:40 PM, David Mertz  wrote:
> 
> I like much of the thinking in Random's approach. But I still think None 
> isn't quite special enough to warrant it's own syntax.
> 
> However, his '(or None: name.strip()[4:].upper())' makes me realize that what 
> is being asked in all the '?(', '?.', '?[' syntax ideas is a kind of ternary 
> expression.  Except the ternary isn't based on whether a predicate holds, but 
> rather on whether an exception occurs (AttributeError, KeyError, TypeError).  
> And the fallback in the ternary is always None rather than being general.
> 
> I think we could generalize this to get something both more Pythonic and more 
> flexible.  E.g.:
> 
> val = name.strip()[4:].upper() except None
> 
> This would just be catching all errors, which is perhaps too broad.  But it 
> *would* allow a fallback other than None:
> 
> val = name.strip()[4:].upper() except -1
> 
> I think some syntax could be possible to only "catch" some exceptions and let 
> others propagate.  Maybe:
> 
> val = name.strip()[4:].upper() except (AttributeError, KeyError): -1
> 
> I don't really like throwing a colon in an expression though.  Perhaps some 
> other word or symbol could work instead.  How does this read:
> 
> val = name.strip()[4:].upper() except -1 in (AttributeError, KeyError)
> 
> Where the 'in' clause at the end would be optional, and default to 
> 'Exception'.
> 
> I'll note that what this idea DOES NOT get us is:
> 
>   val = timeout ?? local_timeout ?? global_timeout
> 
> Those values that are "possibly None" don't raise exceptions, so they 
> wouldn't apply to this syntax.

See the rejected PEP 463 for Exception catching expressions. 

Eric. 

> 
> Yours, David...
> 
> 
>> On Wed, Nov 29, 2017 at 9:03 AM, Random832  wrote:
>> On Tue, Nov 28, 2017, at 15:31, Raymond Hettinger wrote:
>> >
>> > > I also cc python-dev to see if anybody here is strongly in favor or 
>> > > against this inclusion.
>> >
>> > Put me down for a strong -1.   The proposal would occasionally save a few
>> > keystokes but comes at the expense of giving Python a more Perlish look
>> > and a more arcane feel.
>> >
>> > One of the things I like about Python is that I can walk non-programmers
>> > through the code and explain what it does.  The examples in PEP 505 look
>> > like a step in the wrong direction.  They don't "look like Python" and
>> > make me feel like I have to decrypt the code to figure-out what it does.
>> >
>> > timeout ?? local_timeout ?? global_timeout
>> > 'foo' in (None ?? ['foo', 'bar'])
>> > requested_quantity ?? default_quantity * price
>> > name?.strip()[4:].upper()
>> > user?.first_name.upper()
>> 
>> Since we're looking at different syntax for the ?? operator, I have a
>> suggestion for the ?. operator - and related ?[] and ?() that appeared
>> in some of the proposals. How about this approach?
>> 
>> Something like (or None: ...) as a syntax block in which any operation
>> [lexically within the expression, not within e.g. called functions, so
>> it's different from simply catching AttributeError etc, even if that
>> could be limited to only catching when the operand is None] on None that
>> is not valid for None will yield None instead.
>> 
>> This isn't *entirely* equivalent, but offers finer control.
>> 
>> v = name?.strip()[4:].upper() under the old proposal would be more or
>> less equivalent to:
>> 
>> v = name.strip()[4:].upper() if name is not None else None
>> 
>> Whereas, you could get the same result with:
>> (or None: name.strip()[4:].upper())
>> 
>> Though that would technically be equivalent to these steps:
>> v = name.strip if name is not None else None
>> v = v() if v "
>> v = v[4:] """
>> v = v.upper """
>> v = v() """
>> 
>> The compiler could optimize this case since it knows none of the
>> operations are valid on None. This has the advantage of being explicit
>> about what scope the modified rules apply to, rather than simply
>> implicitly being "to the end of the chain of dot/bracket/call operators"
>> 
>> It could also be extended to apply, without any additional syntax, to
>> binary operators (result is None if either operand is None) (or None: a
>> + b), for example, could return None if either a or b is none.
>> 
>> [I think I proposed this before with the syntax ?(...), the (or None:
>> ...) is just an idea to make it look more like Python.]
>> ___
>> Python-Dev mailing list
>> python-...@python.org
>> https://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe: 
>> https://mail.python.org/mailman/options/python-dev/mertz%40gnosis.cx
> 
> 
> 
> -- 
> Keeping medicines from the bloodstreams of the sick; food 
> from the bellies of the hungry; books from the hands of the 
> uneducated; technology from the underdeveloped; and putting 
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century 

Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread David Mertz
I like much of the thinking in Random's approach. But I still think None
isn't quite special enough to warrant it's own syntax.

However, his '(or None: name.strip()[4:].upper())' makes me realize that
what is being asked in all the '?(', '?.', '?[' syntax ideas is a kind of
ternary expression.  Except the ternary isn't based on whether a predicate
holds, but rather on whether an exception occurs (AttributeError, KeyError,
TypeError).  And the fallback in the ternary is always None rather than
being general.

I think we could generalize this to get something both more Pythonic and
more flexible.  E.g.:

val = name.strip()[4:].upper() except None

This would just be catching all errors, which is perhaps too broad.  But it
*would* allow a fallback other than None:

val = name.strip()[4:].upper() except -1

I think some syntax could be possible to only "catch" some exceptions and
let others propagate.  Maybe:

val = name.strip()[4:].upper() except (AttributeError, KeyError): -1

I don't really like throwing a colon in an expression though.  Perhaps some
other word or symbol could work instead.  How does this read:

val = name.strip()[4:].upper() except -1 in (AttributeError, KeyError)

Where the 'in' clause at the end would be optional, and default to
'Exception'.

I'll note that what this idea DOES NOT get us is:

  val = timeout ?? local_timeout ?? global_timeout

Those values that are "possibly None" don't raise exceptions, so they
wouldn't apply to this syntax.

Yours, David...


On Wed, Nov 29, 2017 at 9:03 AM, Random832  wrote:

> On Tue, Nov 28, 2017, at 15:31, Raymond Hettinger wrote:
> >
> > > I also cc python-dev to see if anybody here is strongly in favor or
> against this inclusion.
> >
> > Put me down for a strong -1.   The proposal would occasionally save a few
> > keystokes but comes at the expense of giving Python a more Perlish look
> > and a more arcane feel.
> >
> > One of the things I like about Python is that I can walk non-programmers
> > through the code and explain what it does.  The examples in PEP 505 look
> > like a step in the wrong direction.  They don't "look like Python" and
> > make me feel like I have to decrypt the code to figure-out what it does.
> >
> > timeout ?? local_timeout ?? global_timeout
> > 'foo' in (None ?? ['foo', 'bar'])
> > requested_quantity ?? default_quantity * price
> > name?.strip()[4:].upper()
> > user?.first_name.upper()
>
> Since we're looking at different syntax for the ?? operator, I have a
> suggestion for the ?. operator - and related ?[] and ?() that appeared
> in some of the proposals. How about this approach?
>
> Something like (or None: ...) as a syntax block in which any operation
> [lexically within the expression, not within e.g. called functions, so
> it's different from simply catching AttributeError etc, even if that
> could be limited to only catching when the operand is None] on None that
> is not valid for None will yield None instead.
>
> This isn't *entirely* equivalent, but offers finer control.
>
> v = name?.strip()[4:].upper() under the old proposal would be more or
> less equivalent to:
>
> v = name.strip()[4:].upper() if name is not None else None
>
> Whereas, you could get the same result with:
> (or None: name.strip()[4:].upper())
>
> Though that would technically be equivalent to these steps:
> v = name.strip if name is not None else None
> v = v() if v "
> v = v[4:] """
> v = v.upper """
> v = v() """
>
> The compiler could optimize this case since it knows none of the
> operations are valid on None. This has the advantage of being explicit
> about what scope the modified rules apply to, rather than simply
> implicitly being "to the end of the chain of dot/bracket/call operators"
>
> It could also be extended to apply, without any additional syntax, to
> binary operators (result is None if either operand is None) (or None: a
> + b), for example, could return None if either a or b is none.
>
> [I think I proposed this before with the syntax ?(...), the (or None:
> ...) is just an idea to make it look more like Python.]
> ___
> Python-Dev mailing list
> python-...@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> mertz%40gnosis.cx
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread David Mertz
I didn't say no use case exists, but rather "too special case." And/or
shortcutting is great, but truthiness feels much more general than noneness
to me.

But yes, 'first_non_none()" might have to address laziness in some manner,
depending on the use case. Zero argument lambdas and expression quoting are
the usual hacks (yes, both ugly in their own ways).

On Nov 29, 2017 12:14 AM, "Nick Coghlan"  wrote:

On 29 November 2017 at 16:13, David Mertz  wrote:
> Strong -1 still from me. Too special case for syntax. Just write a
function
> 'first_non_none()' that can perfectly will handle the need.

That's the equivalent of SQL's COALESCE, and it's insufficient for the
same reason "and" and "or" are syntax rather than builtins: the
function form doesn't provide short-circuiting behaviour.

As far as utility goes, I put it in a similar category to matrix
multiplication: if you don't need it, you don't need it, but when you
do need it, you need it a *lot*.

The use case where these operations come up is when you're working
with partially structured hierarchical data (*cough*JSON*cough*). In
those kinds of structures, None is frequently used as a marker to say
"this entire subtree is missing", and you either want to propagate
that None, or else replace it with something else (and the "something
else" may be a network call to a different service, so you definitely
don't want to do it if you don't need to).

So I'd remind folks to try to avoid the "I don't need this, so nobody
needs this" mistake. It's OK to say "the use case exists, but I still
don't want that particular syntax for it in Python" (I'm personally
inclined to agree with you on that front). It's not OK to try to claim
there are no use cases where the status quo is awkward enough to
become irritating (since it's an empirically false statement that you
don't need to be making).

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Elazar
On Wed, Nov 29, 2017 at 3:12 PM Serhiy Storchaka 
wrote:

> 29.11.17 15:01, Stephan Houben пише:
> > What about a more general:
> >
> > A if  B else C
> >
> > which would allow
> >
> > A if is not None else C
> >
> > but also e.g.
> >
> > A if >= 1 else 0
>
> This look the most "natural" to me. I.e. the least "unnatural". If we
> even will introduce a new special syntax I will prefer this syntax.
>
> The only disadvantage is that this will prevent introducing "angular
> parenthesis" in future: A if  == B else C.
>
>
Also, how do you treat more complex conditions? do you prohibit them?

A if >= 0 and < 2 else 0
B if >= 0 or < -1 else -1
C if and B else A   # i.e. C if C and B else E
D if -y else E  # is this an unary minus or a binary operation?

If any of these is allowed, it might confuse people.
It also break the ability to extract subexpression to variables.

(I kinda like this idea, but it doesn't look like a small, simple change to
the language, whereas ?? does).

Elazar
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Serhiy Storchaka

29.11.17 15:01, Stephan Houben пише:

What about a more general:

A if  B else C

which would allow

A if is not None else C

but also e.g.

A if >= 1 else 0


This look the most "natural" to me. I.e. the least "unnatural". If we 
even will introduce a new special syntax I will prefer this syntax.


The only disadvantage is that this will prevent introducing "angular 
parenthesis" in future: A if  == B else C.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Paul Moore
On 29 November 2017 at 12:41, Nick Coghlan  wrote:
> On 29 November 2017 at 22:38, Stephan Houben  wrote:
>> What about more English-like syntax:
>>
>> X or else Y
>
> The problem with constructs like this is that they look like they
> should mean the same thing as "X or Y".

Keyword based and multi-word approaches also break down much faster
when you get more terms.

X or else Y

looks OK (ignoring Nick's comment - I could pick another keyword-based
proposal, but I'm too lazy to look for one I like), but when you have
4 options,

X or else Y or else Z or else W

the benefit isn't as obvious. Use lower-case and longer names

item_one or else item_two or else list_one[the_index] or dict_one['key_one']

and it becomes just a muddle of words.

Conversely, punctuation-based examples look worse with shorter
variables and with expressions rather than identifiers:

item_one ?? item_two ?? another_item ?? one_more_possibility

vs

x ?? y[2] ?? kw['id'] ?? 3 + 7

IMO, this is a case where artificial examples are unusually bad at
conveying the actual feel of a proposal. It's pretty easy to turn
someone's acceptable-looking example into an incomprehensible mess,
just by changing variable names and example terms. So I think it's
critically important for any proposal along these lines (even just
posts to the mailing list, and definitely for a PEP), that it's argued
in terms of actual code examples in current projects that would
reasonably be modified to use the proposed syntax. And people wanting
to be particularly honest in their proposals should probably include
both best-case and worst-case examples of readability.

Paul

PS Also, I want a pony. (I really do understand that the above is not
realistic, but maybe I can hope that at least anyone writing a PEP
take it into consideration :-))
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Serhiy Storchaka

29.11.17 14:39, Nick Coghlan пише:

 "a if def else b" -> pronounced "a if defined, else b"


I understand "a if defined, else b" as

try:
result = a
except (NameError, AttributeError, LookupError):
result = b

The problem is that None is not undefined. This is a regular value. 
Seems it is not so special in Python as NULL or undef in other 
languages. Python even don't have a special purposed 
NullPointerException (or NoneError).


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Stephan Houben
What about a more general:

A if  B else C

which would allow

A if is not None else C

but also e.g.

A if >= 1 else 0

Stephan

Op 29 nov. 2017 13:41 schreef "Nick Coghlan" :

> On 29 November 2017 at 22:38, Stephan Houben  wrote:
> > What about more English-like syntax:
> >
> > X or else Y
>
> The problem with constructs like this is that they look like they
> should mean the same thing as "X or Y".
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Nick Coghlan
On 29 November 2017 at 22:38, Stephan Houben  wrote:
> What about more English-like syntax:
>
> X or else Y

The problem with constructs like this is that they look like they
should mean the same thing as "X or Y".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Nick Coghlan
On 29 November 2017 at 21:53, Serhiy Storchaka  wrote:
> 29.11.17 11:45, Steven D'Aprano пише:
>> On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:
>>> What is the syntax of the ternary operator in these languages?
>>
>> All four use:
>>
>>  condition ? first : second
>>
>> for the ternary if operator.
>
> If all four use ?, it is natural that in operators which are shortcuts of
> the ternary operator they use ?. But in Python the bar of introducing ? is
> higher.

It's also noteworthy that they all offer "&&" and "||" as short
circuiting "and" and "or" operators. When you have that pattern
established, then "??" fits right in.

With Python's only spelling for the logical operators being "and" and
"or", the cryptic unpronounceable nature of "??" becomes much harder
to ignore.

   "a and b" -> pronounced "a and b"
   "a or b" -> pronounced "a or b"
   "a ?? b" -> pronounced "a  b"

The only potentially pronounceable versions I've been able to come up
with are a fair bit longer:

"a if def else b" -> pronounced "a if defined, else b"
"a if ?? is not None else b" -> pronounced "a if the leftmost
operand is not None, else b"
"a if ?? is None else ??.b" -> pronounced "a if the leftmost
operand is None, else the leftmost operand dot b"

(The last two examples there are a version where "??" appearing in
either the condition expression or the RHS of a conditional expression
would cause the leftmost operand to be eagerly evaluated, placed in a
hidden temporary variable and then referenced multiple times as a
subexpression. A similar construct could then also be used in filter
expressions in comprehensions to refer back to the loop's result
clause: "[f(x) for x in iterable if ?? is not None]". It's still
magical and hard to look up syntax, but "??" as a magic placeholder to
say "Reference the leftmost operand in the current expression here,
but still only evaluate it once" seems nicer to me than using it as a
cryptic binary operator)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Stephan Houben
What about more English-like syntax:

X or else Y

E.g.

cache.get(foo) or else expensive_call(foo)

Stephan


Op 29 nov. 2017 12:54 schreef "Serhiy Storchaka" :

29.11.17 11:45, Steven D'Aprano пише:

On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:
>
>> 29.11.17 08:08, Steven D'Aprano пише:
>>
>>> Perl is hardly the only language with null-coalescing operators -- we
>>> might better describe ?? as being familiar to C#, PHP, Swift and Dart.
>>> That's two mature, well-known languages and two up-and-coming languages.
>>>
>>
>> What is the syntax of the ternary operator in these languages?
>>
>
> All four use:
>
>  condition ? first : second
>
> for the ternary if operator.
>

If all four use ?, it is natural that in operators which are shortcuts of
the ternary operator they use ?. But in Python the bar of introducing ? is
higher.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Serhiy Storchaka

29.11.17 11:45, Steven D'Aprano пише:

On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:

29.11.17 08:08, Steven D'Aprano пише:

Perl is hardly the only language with null-coalescing operators -- we
might better describe ?? as being familiar to C#, PHP, Swift and Dart.
That's two mature, well-known languages and two up-and-coming languages.


What is the syntax of the ternary operator in these languages?


All four use:

 condition ? first : second

for the ternary if operator.


If all four use ?, it is natural that in operators which are shortcuts 
of the ternary operator they use ?. But in Python the bar of introducing 
? is higher.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Elazar
בתאריך יום ד׳, 29 בנוב׳ 2017, 12:29, מאת Lele Gaifax ‏:

> Kirill Balunov  writes:
>
> > Since the proposed semantics is more similar to the idea of "or", may be
> it
> > is better to consider something like:
> >
> > timeout then local_timeout then global_timeout
> >
> > I do not know how much this is a frequent case to be worthy of a keyword.
>
> That sounds awkward... what about
>
>   timeout else local_timeout else global_timeout
>
> instead?
>

I think then and else can be ruled out since they can be very confusing
when used in a conditinal statement or a comprehension.

x = a if x else y else b  # wait. what?

Elazar
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Lele Gaifax
Kirill Balunov  writes:

> Since the proposed semantics is more similar to the idea of "or", may be it
> is better to consider something like:
>
> timeout then local_timeout then global_timeout
>
> I do not know how much this is a frequent case to be worthy of a keyword.

That sounds awkward... what about

  timeout else local_timeout else global_timeout

instead?

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Elazar
בתאריך יום ד׳, 29 בנוב׳ 2017, 11:46, מאת Steven D'Aprano ‏<
st...@pearwood.info>:

> On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:
> > 29.11.17 08:08, Steven D'Aprano пише:
> > >Perl is hardly the only language with null-coalescing operators -- we
> > >might better describe ?? as being familiar to C#, PHP, Swift and Dart.
> > >That's two mature, well-known languages and two up-and-coming languages.
> >
> > What is the syntax of the ternary operator in these languages?
>
> All four use:
>
> condition ? first : second
>
> for the ternary if operator.
>

This suggests something like "ifNone" keyword :

x = a ifNone b ifNone c

Elazar
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Steven D'Aprano
On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:
> 29.11.17 08:08, Steven D'Aprano пише:
> >Perl is hardly the only language with null-coalescing operators -- we
> >might better describe ?? as being familiar to C#, PHP, Swift and Dart.
> >That's two mature, well-known languages and two up-and-coming languages.
> 
> What is the syntax of the ternary operator in these languages?

All four use:

condition ? first : second

for the ternary if operator.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Nick Coghlan
On 29 November 2017 at 18:49, Kirill Balunov  wrote:
> 2017-11-29 11:14 GMT+03:00 Nick Coghlan :
>>
>> It's OK to say "the use case exists, but I still
>> don't want that particular syntax for it in Python" (I'm personally
>> inclined to agree with you on that front). It's not OK to try to claim
>> there are no use cases where the status quo is awkward enough to
>> become irritating (since it's an empirically false statement that you
>> don't need to be making).
>
> If the problem with the proposed syntax, but there are cases for use, it may
> be worth to bikeshed one more time?

I don't think enough time has passed since we first discussed it.
Since Guido has already said "not for 3.7", we can give folks another
12+ months to ponder the problem, and then maybe revisit it for 3.8.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Kirill Balunov
2017-11-29 11:14 GMT+03:00 Nick Coghlan :


> It's OK to say "the use case exists, but I still
> don't want that particular syntax for it in Python" (I'm personally
> inclined to agree with you on that front). It's not OK to try to claim
> there are no use cases where the status quo is awkward enough to
> become irritating (since it's an empirically false statement that you
> don't need to be making).
>

If the problem with the proposed syntax, but there are cases for use, it
may be worth to bikeshed one more time?

2017-11-29 9:08 GMT+03:00 Steven D'Aprano :

I'd much prefer to read, write and teach the version with ?? over the
> status quo.


Since the proposed semantics is more similar to the idea of "or", may be it
is better to consider something like:

timeout then local_timeout then global_timeout

I do not know how much this is a frequent case to be worthy of a keyword.

With kind regards, -gdg

2017-11-29 11:14 GMT+03:00 Nick Coghlan :

> On 29 November 2017 at 16:13, David Mertz  wrote:
> > Strong -1 still from me. Too special case for syntax. Just write a
> function
> > 'first_non_none()' that can perfectly will handle the need.
>
> That's the equivalent of SQL's COALESCE, and it's insufficient for the
> same reason "and" and "or" are syntax rather than builtins: the
> function form doesn't provide short-circuiting behaviour.
>
> As far as utility goes, I put it in a similar category to matrix
> multiplication: if you don't need it, you don't need it, but when you
> do need it, you need it a *lot*.
>
> The use case where these operations come up is when you're working
> with partially structured hierarchical data (*cough*JSON*cough*). In
> those kinds of structures, None is frequently used as a marker to say
> "this entire subtree is missing", and you either want to propagate
> that None, or else replace it with something else (and the "something
> else" may be a network call to a different service, so you definitely
> don't want to do it if you don't need to).
>
> So I'd remind folks to try to avoid the "I don't need this, so nobody
> needs this" mistake. It's OK to say "the use case exists, but I still
> don't want that particular syntax for it in Python" (I'm personally
> inclined to agree with you on that front). It's not OK to try to claim
> there are no use cases where the status quo is awkward enough to
> become irritating (since it's an empirically false statement that you
> don't need to be making).
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-29 Thread Nick Coghlan
On 29 November 2017 at 16:13, David Mertz  wrote:
> Strong -1 still from me. Too special case for syntax. Just write a function
> 'first_non_none()' that can perfectly will handle the need.

That's the equivalent of SQL's COALESCE, and it's insufficient for the
same reason "and" and "or" are syntax rather than builtins: the
function form doesn't provide short-circuiting behaviour.

As far as utility goes, I put it in a similar category to matrix
multiplication: if you don't need it, you don't need it, but when you
do need it, you need it a *lot*.

The use case where these operations come up is when you're working
with partially structured hierarchical data (*cough*JSON*cough*). In
those kinds of structures, None is frequently used as a marker to say
"this entire subtree is missing", and you either want to propagate
that None, or else replace it with something else (and the "something
else" may be a network call to a different service, so you definitely
don't want to do it if you don't need to).

So I'd remind folks to try to avoid the "I don't need this, so nobody
needs this" mistake. It's OK to say "the use case exists, but I still
don't want that particular syntax for it in Python" (I'm personally
inclined to agree with you on that front). It's not OK to try to claim
there are no use cases where the status quo is awkward enough to
become irritating (since it's an empirically false statement that you
don't need to be making).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-28 Thread Serhiy Storchaka

29.11.17 08:08, Steven D'Aprano пише:

Perl is hardly the only language with null-coalescing operators -- we
might better describe ?? as being familiar to C#, PHP, Swift and Dart.
That's two mature, well-known languages and two up-and-coming languages.


What is the syntax of the ternary operator in these languages?

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-28 Thread David Mertz
Strong -1 still from me. Too special case for syntax. Just write a function
'first_non_none()' that can perfectly will handle the need.

On Nov 28, 2017 10:09 PM, "Steven D'Aprano"  wrote:

> On Tue, Nov 28, 2017 at 12:31:06PM -0800, Raymond Hettinger wrote:
> >
> > > I also cc python-dev to see if anybody here is strongly in favor or
> against this inclusion.
> >
> > Put me down for a strong -1.  The proposal would occasionally save a
> > few keystokes but comes at the expense of giving Python a more Perlish
> > look and a more arcane feel.
>
> I think that's an unfair characterisation of the benefits of the PEP.
> It's not just "a few keystrokes".
>
> Ironically, the equivalent in Perl is // which Python has used for
> truncating division since version 2.4 or so. So if we're in danger of
> looking "Perlish", that ship has sailed a long time ago.
>
> Perl is hardly the only language with null-coalescing operators -- we
> might better describe ?? as being familiar to C#, PHP, Swift and Dart.
> That's two mature, well-known languages and two up-and-coming languages.
>
>
> [...]
> > timeout ?? local_timeout ?? global_timeout
>
> As opposed to the status quo:
>
> timeout if timeout is not None else (local_timeout if local_timeout is
> not None else global_timeout)
>
> Or shorter, but even harder to understand:
>
> (global_timeout if local_timeout is None else local_timeout) if
> timeout is None else timeout
>
> I'd much prefer to teach the version with ?? -- it has a simple
> explanation: "the first of the three given values which isn't None". The
> ?? itself needs to be memorized, but that's no different from any other
> operator. The first time I saw ** I was perplexed and couldn't imagine
> what it meaned.
>
> Here ?? doesn't merely save a few keystrokes, it significantly reduces
> the length and complexity of the expression and entirely cuts out the
> duplication of names.
>
> If you can teach
>
> timeout or local_timeout or global_timeout
>
> then you ought to be able to teach ??, as it is simpler: it only
> compares to None, and avoids needing to explain or justify Python's
> truthiness model.
>
>
> > 'foo' in (None ?? ['foo', 'bar'])
>
> If you can understand
>
> 'foo' in (False or ['foo', 'bar'])
>
> then surely you can understand the version with ??.
>
>
> > requested_quantity ?? default_quantity * price
>
> Again:
>
> (default_quantity if requested_quantity is None else
> requested_quantity) * price
>
>
> I'd much prefer to read, write and teach the version with ?? over the
> status quo.
>
>
> --
> Steve
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

2017-11-28 Thread Steven D'Aprano
On Tue, Nov 28, 2017 at 12:31:06PM -0800, Raymond Hettinger wrote:
> 
> > I also cc python-dev to see if anybody here is strongly in favor or against 
> > this inclusion.
> 
> Put me down for a strong -1.  The proposal would occasionally save a 
> few keystokes but comes at the expense of giving Python a more Perlish 
> look and a more arcane feel.

I think that's an unfair characterisation of the benefits of the PEP. 
It's not just "a few keystrokes".

Ironically, the equivalent in Perl is // which Python has used for 
truncating division since version 2.4 or so. So if we're in danger of 
looking "Perlish", that ship has sailed a long time ago.

Perl is hardly the only language with null-coalescing operators -- we 
might better describe ?? as being familiar to C#, PHP, Swift and Dart. 
That's two mature, well-known languages and two up-and-coming languages.


[...]
> timeout ?? local_timeout ?? global_timeout

As opposed to the status quo:

timeout if timeout is not None else (local_timeout if local_timeout is not 
None else global_timeout)

Or shorter, but even harder to understand:

(global_timeout if local_timeout is None else local_timeout) if timeout is 
None else timeout

I'd much prefer to teach the version with ?? -- it has a simple 
explanation: "the first of the three given values which isn't None". The 
?? itself needs to be memorized, but that's no different from any other 
operator. The first time I saw ** I was perplexed and couldn't imagine 
what it meaned.

Here ?? doesn't merely save a few keystrokes, it significantly reduces 
the length and complexity of the expression and entirely cuts out the 
duplication of names.

If you can teach 

timeout or local_timeout or global_timeout

then you ought to be able to teach ??, as it is simpler: it only 
compares to None, and avoids needing to explain or justify Python's 
truthiness model.


> 'foo' in (None ?? ['foo', 'bar'])

If you can understand

'foo' in (False or ['foo', 'bar'])

then surely you can understand the version with ??.


> requested_quantity ?? default_quantity * price

Again:

(default_quantity if requested_quantity is None else requested_quantity) * 
price


I'd much prefer to read, write and teach the version with ?? over the 
status quo.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/