Re: [Python-Dev] PEP 572: Usage of assignment expressions in C

2018-04-30 Thread Tim Peters
[Raymond Hettinger ]
> Thanks Antoine, this is an important point that I hope doesn't get lost.
> In a language with exceptions, assignment expressions are less needful.
> Also, the pattern of having of having mutating methods return None
> further limits the utility.

It doesn't diminish the utility one whit in cases where binding
expressions are helpful ;-)

What you're saying is that there are _fewer_ such opportunities in
Python than in C.  Which may or may not be true (depending on the code
you're working with).  If you believe it is true, fine, then that also
argues against that people will rush to abuse the feature (to the
extent that it's even plausibly useful less often, to that extent also
will there be less temptation to use it at all).

But then I only care about use cases at heart, and have presented
real-life examples wherein binding expressions read both better and
worse than what they're replacing.  I intend to limit myself to the
cases where they read better :-)  Which are most of the cases I even
considered, BTW - in the vast majority of cases in real code I'd use
them, they'd be replacing the annoyingly bare-bones yet somehow
repetitive anyway:

value = f()
if value;
doing something with value

with the still bare-bones but minimally repetitive:

if value := f():
doing something with value

For example, tons of functions I write and use return None or 0 or
False when they want to communicate "I have nothing useful to return
in this case - but since you expected that might happen, I'm not going
to annoy you with an exception".  That pattern isn't solely limited to
regexp search and match functions.

The "win" above is minor but frequent.  It adds up.

There are other cases where binding expressions really shine, but
they're much rarer in all the code I looked at (e.g., see the
uselessly ever-increasing indentation levels near the end of `copy()`
in the std library's copy.py).

In all, I expect I'd use them significantly more often than ternary
`if`, but far less often than augmented assignments.  If the PEP is
accepted, that's what all Pythoneers will be saying 5 years from now
;-)
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 572: Usage of assignment expressions in C

2018-04-30 Thread Raymond Hettinger


> On Apr 28, 2018, at 8:45 AM, Antoine Pitrou  wrote:
> 
>> I personally haven't written a lot of C, so have no personal experience,
>> but if this is at all a common approach among experienced C developers, it
>> tells us a lot.
> 
> I think it's a matter of taste and personal habit.  Some people will
> often do it, some less.  Note that C also has a tendency to make it
> more useful, because doesn't have exceptions, so functions need to
> (ab)use return values when they want to indicate an error.  When you're
> calling such functions (for example I/O functions), you routinely have
> to check for special values indicating an error, so it's common to see
> code such as:
> 
>  // Read up to n bytes from file descriptor
>  if ((bytes_read = read(fd, buf, n)) == -1) {
>  // Error occurred while reading, do something
>  }

Thanks Antoine, this is an important point that I hope doesn't get lost.
In a language with exceptions, assignment expressions are less needful.
Also, the pattern of having of having mutating methods return None
further limits the utility.


Raymond
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 572: Usage of assignment expressions in C

2018-04-30 Thread Victor Stinner
2018-04-28 17:45 GMT+02:00 Antoine Pitrou :
>   // Read up to n bytes from file descriptor
>   if ((bytes_read = read(fd, buf, n)) == -1) {
>   // Error occurred while reading, do something
>   }

About C, there is a popular coding style (not used by our PEP 7) for comparison:

   if (-1 == n) ...

The advantage is to prevent typo mistakes because "-1 = n" is a syntax error.

Victor
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 572: Usage of assignment expressions in C

2018-04-28 Thread Antoine Pitrou
On Fri, 27 Apr 2018 09:49:33 -0700
Chris Barker  wrote:
> On Tue, Apr 24, 2018 at 2:21 AM, Victor Stinner  wrote:
> 
> > Even if the C language allows assignments in if, I avoid them, because
> > I regularly have to debug my own code in gdb ;-)
> >  
> 
> I personally haven't written a lot of C, so have no personal experience,
> but if this is at all a common approach among experienced C developers, it
> tells us a lot.

I think it's a matter of taste and personal habit.  Some people will
often do it, some less.  Note that C also has a tendency to make it
more useful, because doesn't have exceptions, so functions need to
(ab)use return values when they want to indicate an error.  When you're
calling such functions (for example I/O functions), you routinely have
to check for special values indicating an error, so it's common to see
code such as:

  // Read up to n bytes from file descriptor
  if ((bytes_read = read(fd, buf, n)) == -1) {
  // Error occurred while reading, do something
  }


Regards

Antoine.


___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com