Naive idiom questions



* 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.
.



Relevant Pages

  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • chapter4
    ... The for statement in Python differs a bit from what you may be used to ... list or a string), in the order that they appear in the sequence. ... (this can only happen for mutable sequence types, such as lists). ... The keyword def introduces a function definition. ...
    (Ubuntu)
  • Re: Newcomer question wrt variable scope/namespaces
    ... that actually the rule has to be refined as pertaining to the "immutable" types whereas lists and dictionaries are "mutable" types and the said scoping rule does not apply? ... references to objects in Python. ... tied to a string ... but your labels are neatly organized in various namespaces. ...
    (comp.lang.python)
  • Re: Can a method in one class change an object in another class?
    ... A variable in python is just a name refering to an object. ... will make a and b both refer to the string 'a'. ... distinction between mutable objects and immutable ones. ... lists and dicts and objects of the former. ...
    (comp.lang.python)
  • Re: Newbi Q: What is a rational for strings not being lists in Python?
    ... reverseis a lucky one - Python has variants of *this particular* ... function both for lists and strings. ... can I write a function that works both on list and string ... Yet when I try to reverse a string I get: ...
    (comp.lang.python)