Why " ".some_string is often used ?
Erik Max Francis
max at alcyone.com
Thu Jan 8 12:50:04 CET 2004
John Roth wrote:
> And I agree, it's not entirely obvious why it's a string
> method rather than a list method, since it operates on
> a list, not on a string. The only explanation that makes
> sense is that, as a list method, it would fail if the list
> contained something other than a string. That's still
> not very friendly, though.
On the contrary, I think that's the best reason. Lists have nothing to
do with strings, and so very string-specific methods (discounting
system-wide things such as str or repr) being included in lists is not
the right approach. Furthermore, the methods associated with a list
tend to become the "pattern" that sequence types must fulfill, and it
sets a terribly bad precedent to attach whatever domain-specific
application that's needed into a sequence type just because it's easiest
on the eyes at the moment.
The .join method is inherently string specific, and belongs on strings,
not lists. There's no doubting that seeing S.join(...) for the first
time is a bit of a surprise, but once you understand the reasoning
behind it, it makes perfect sense and makes it clear just how much it
deserves to stay that way.
And above all, of course, if you think it personally looks ugly, you can
from string import join
or write your own join function that operates over sequences and does
whatever else you might wish. That's what the flexibility is there for.
__ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Life is not a spectacle or a feast; it is a predicament.
-- George Santayana
More information about the Python-list