new linereading standard?

Juergen A. Erhard jae at ilk.de
Tue Apr 25 20:26:14 EDT 2000


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>>> "Johann" == Johann Hibschman <johann at physics.berkeley.edu> writes:

[J W B]

    Johann> To go off on a wild tangent, this reminds me of the whole
    Johann> map/lambda functional programming fracas.  See, I agree
    Johann> with most people that I don't really want to see code like

    Johann>   map(lambda s: string.split(string.strip(s), ',')[0],
    Johann>       open('data.dat').readlines())

    Johann> I much prefer

    Johann>   out = []
    Johann>   for s in open('data.dat').readlines():
    Johann>      fields = string.split(string.strip(s), ',')
    Johann>      out.append(fields[0])

    Johann> But, if I'm doing this at the interactive prompt, what I end up typing
    Johann> is most often something like:

    >>>> lines = open('data.dat').readlines()
    >>>> lines = map(string.strip, lines)
    >>>> lines = map(lambda l: string.split(l, ','), lines)
    >>>> out   = map(lambda fs: fs[0], lines)

    Johann> So, basically, I find the functional stuff much more
    Johann> useful from the command line than I do from actual
    Johann> programs.  I think of the right transform, then I apply
    Johann> it.  Interesting.

Actually, I find the third example more readable than the other two...

   map(string.strip, lines)

is, IMHO, more readable than a for loop doing the same.

Conciseness *can* improve readability.

Though it also depends on what you're used to... (maps etc are *more*
readable in Lisp... than in Python)

    Johann> Heh.  I'm free-associating here, please bear with
    Johann> me. Anyone else have this experience?

Free-associating?  Yes, I know that experience... ;-)

Bye, J

PS: /me learned lisp a pretty long time ago from a four-part article
in `Scientific American' by Douglas Hofstadter... :-) (Actually, it
was a German translation in `Spektrum der Wissenschaft')

- -- 
Jürgen A. Erhard      eMail: jae at ilk.de      phone: (GERMANY) 0721 27326
          My WebHome: http://members.tripod.com/Juergen_Erhard
           "Outside of a dog, a man's best friend is a book;
           inside of a dog, it's very dark." --  Groucho Marx
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.1 (GNU/Linux)
Comment: Use Mailcrypt and GnuPG <http://www.gnupg.org/>

iEYEARECAAYFAjkGN6UACgkQN0B+CS56qs1+mwCfa9+imyRXQaLqwLXzb0uCkc0W
XbYAoJAlRQENPIdp6dUZqvi8oIwCuaeL
=7ZPG
-----END PGP SIGNATURE-----




More information about the Python-list mailing list