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

John Posner jjposner at snet.net
Wed Apr 1 13:44:01 EDT 2009


 >> >    mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"),
block)
 >> >
 >> 
 >> Never tested my 'pythonicity', but I would do:
 >> 
 >> def test(b) : b > 127 or b in r"\r\n\t"

Oops! Clearly, 

   b in "\r\n\t"

.... is preferable to ...

   b in list("\r\n\t")


You do *not* want to use a raw string here:

>>> len ("\n\r\t")
3
>>> len (r"\n\r\t")
6





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