SPAM-LOW: Re: Function/method returning list of chars in string?

Chris Rebert clp2 at rebertia.com
Tue Jun 9 06:02:43 EDT 2009


On Tue, Jun 9, 2009 at 2:42 AM, Hendrik van Rooyen<mail at microcorp.co.za> wrote:
> "Chris Rebert" <clp2 at ria.com> wrote:
>
>> lb = list("banana")
>
> Aaargh!
>
> I should have known - you use a string method to get a list of words,
> but you have to go to the list to get a list of characters from a string.
> There is no string method to do it, which is what I am complaining
> about.
>
> Is there a reason for this, or is the lack of symmetry just an historical
> artefact?

I think most would agree that having join() be a method of `str`
rather than `list` can be understandably confusing to newbies, so the
apparent asymmetry lies on the other side of the list-str boundary
(i.e. why don't I use a list method to convert a list of strings to a
string?).
The reason join() is a method of `str` is so that it doesn't have to
be reimplemented for every container type (e.g. tuple, set, list,
user-defined containers, etc).
You can look at it the same way for why a `str` method isn't used to
produce a list of characters -- what would you do for every other
container type (i.e. what if I want a set of the characters in a
string)? Having the container type rather than the string take care of
it solves this problem.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list