Which is more Pythonic? (was: Detecting Binary content in files)

John Posner jjposner at snet.net
Wed Apr 1 17:10:38 CEST 2009


Dennis Lee Bieber presented a code snippet with two consecutive statements
that made me think, "I'd code this differently". So just for fun ... is
Dennis's original statement or my "_alt" statement more idiomatically
Pythonic? Are there even more Pythonic alternative codings?

   mrkrs = [b for b in block 
     if b > 127 
       or b in [ "\r", "\n", "\t" ]	]

   mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ],
block)
   mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block)


(Note: Dennis's statement converts a string into a list; mine does not.)

---
		
   binary = (float(len(mrkrs)) / len(block)) > 0.30

   binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30

-John





E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.12090
http://www.pctools.com/en/spyware-doctor-antivirus/



More information about the Python-list mailing list