List to string? Docstrings?

Skip Montanaro skip at mojam.com
Mon Sep 13 18:02:35 EDT 1999


    François> 1) For converting a string into a list, I used:

    François>         chars = list(text)

    François>    For getting back the string from the list, I tried:

    François>         import operator
    François>         text = reduce(operator.concat, chars)

The normal idiom for smashing a list into a string is

    import string
    text = string.join(chars, "")

but your method is just as effective.  The operator module and the reduce
builtin function are new additions to Python relative to the string module.
I think that would explain why most people use string.join instead of your
solution.

    François> 2) I still miss the purpose of __doc__ strings.  It's true
    François>    that I can write:

    François>         print module.__doc__
    François>         print function.__doc__

    François>    and sometimes get useful information from these.  But what
    François>    would I be loosing, for my own things, if I was using mere
    François>    `#' comments instead?

Only that you deny other programs the opportunity to present you with useful
information.  For example, IDLE could presumably import a module and display
an object's doc string in a popup window if you asked for it.  It couldn't
do that with comments.  Similarly, batch-oriented documentation tools can
assemble a bunch of doc strings together into a more-or-less cohesive whole
based upon traversing a module's dict recursively, emitting the whole mess
as HTML, LaTex, nroff or what-have-you.  There is a tool floating around
called gendoc that does this sort of thing I believe.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list