Naive idiom questions

Terran Melconian te_rem_ra_ove_an_forspam at consistent.org
Thu Jan 31 16:30:09 EST 2008


* Why are there no real mutable strings available?

    I found MutableString in UserString, but further research indicates
    that it is horribly inefficient and actually just wraps immutable
    strings for the implementation.

    I want to be able to accumulate a string with +=, not by going
    through an intermediate list and then doing ''.join(), because I
    think the latter is ugly.  There are also times when I'd like to use
    the string as a modifiable buffer.

    Is the python idiom that this is actually the Right Thing for
    reasons I'm not seeing?  Is there a fundamental reason it would be
    hard to implement a mutable string in cpython?

* What's the best way to initialize a list of lists?

    I keep wanting to do this:

	l=[[None]*5]*5

    as do many other Python novices, but of course it quickly becomes
    apparent why this is a bad idea as soon as one modifies a value.
    The best I've found is:

	l=[[None]*5 for i in range(5)]

    I don't particularly like it, though.  It bothers me to have to
    explain list comprehensions, which are a moderately advanced feature
    conceptually, just to initialize an array.  We could get around this
    if we had a defaultlist, but we don't.  Is there a more elegant
    solution here?

* Is there a way to get headings in docstrings?

    I want to create my own sections, like "OVERVIEW", "EXAMPLES",
    "AUTHORS", "BUGS", etc.  I can't figure out any way to do this.  In
    perldoc, I can easily use =head1, but I can't figure out the Python
    equivalent.  At first I thought I could use (re)structuredtext, but
    then it became apparent that pydoc(1) does not parse any of this
    markup.  Am I missing something, or is it just not possible to
    achieve this effect other than by writing separate, independent
    manpages?

Thanks for any suggestions or thoughts.



More information about the Python-list mailing list