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

Antoine Pitrou solipsis at pitrou.net
Sat Apr 28 11:45:24 EDT 2018


On Fri, 27 Apr 2018 09:49:33 -0700
Chris Barker <chris.barker at noaa.gov> wrote:
> On Tue, Apr 24, 2018 at 2:21 AM, Victor Stinner <vstinner at redhat.com> 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.




More information about the Python-Dev mailing list