Does Python really follow its philosophy of "Readability counts"?

r rt8396 at gmail.com
Wed Jan 14 00:00:29 EST 2009


Here is a piece of C code this same guy showed me saying Pythonic
indention would make this hard to read -- Well lets see then!

I swear, before god, this is the exact code he showed me. If you don't
believe me i will post a link to the thread.

//  Warning ugly C code ahead!
if( is_opt_data() < sizeof( long double ) ) { // test for insufficient
data
    return TRUE; // indicate buffer empty
  } // end test for insufficient data
  if( is_circ() ) { // test for circular buffer
    if( i < o ) { // test for data area divided
      if( ( l - o ) > sizeof( long double ) ) { // test for data
contiguous
        *t = ( ( long double * ) f )[ o ]; // return data
        o += sizeof( long double ); // adjust out
        if( o >= l ) { // test for out wrap around
          o = 0; // wrap out around limit
        } // end test for out wrap around
      } else { // data not contiguous in buffer
        return load( ( char * ) t, sizeof( long double ) ); // return
data
      } // end test for data contiguous
    } else { // data are not divided
      *t = ( ( float * ) f )[ o ]; // return data
      o += sizeof( long double ); // adjust out
      if( o >= l ) { // test for out reached limit
        o = 0; // wrap out around
      } // end test for out reached limit
    } // end test for data area divided
  } else { // block buffer
    *t = ( ( long double * ) f )[ o ]; // return data
    o += sizeof( long double ); // adjust data pointer
  } // end test for circular buffer


if i where to write the same code in a 'Python style" it would look
like below. And personally i would never use that many comments in my
code. I normally in a situation as this one would only comment each
major conditional code block, and only if it contains code that is not
completely obvious. Commenting is important, but it *can* be over
done.

#-- Python Style --#
if is_opt_data() < sizeof(long double):
  return TRUE
  if is_circ():
    if i < o: #test for data area divided
      if (l-o) > sizeof(long double): #test for data contiguous
        *t = ( ( long double * ) f )[ o ]
        o += sizeof( long double )
        if o >= l:
          o = 0
      else: #data not contiguous in buffer
        return load((char*) t, sizeof(long double))
    else: #data are not divided
      *t = ((float*) f)[ o ]
      o += sizeof(long double)
      if o >= l: #test for out reached limit
        o = 0
  else: #block buffer
    *t = ((long double*) f)[ o ]
    o += sizeof(long double)

WOW!, without all the braces, and over commenting,  i can actually
read this code now! Of course it would not run in C or Python but the
point here is readability. Python forged the path for all 21st century
languages. Get on board, or get on with your self.extinction() -- Your
Choice!




More information about the Python-list mailing list