Why " ".some_string is often used ?

Syver Enstad syver-en+usenet at online.no
Thu Jan 8 16:34:39 CET 2004


Peter Hansen <peter at engcorp.com> writes:

> 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.
> 
> One could about as easily argue (and I believe several have done
> this quite well in the past, better than I anyway) that you are
> actually operating on the *string*, not the list.  You are in
> effect asking the string to act as a joiner for the elements in the
> list, not asking the list to join itself using the specified
> string.
> 
> At least, if you look at it that way, it might be easier to swallow.

Can't we have both. This is called a reversing method (Beck, Smalltalk
Best Practice Patterns) because it allows you to send several messages
to the same object instead of switching between different instances,
allowing the code to be more regular.

class MyList(list):
    def join(self, aString):
        return aString.join(self)


Like this:

lst = ['one', 'two', 'three']
print lst
print lst.join('\n')

I'd also like a reversing method for len

class MyList(list):
   def len(self):
        return len(self)

Often when I program against an instance I intuitively start each line
of code by writing the variable name and then a dot and then the
operation. The lack of a reversing method for len and join means that
my concentration is broken a tiny fraction of a second when I have to
remember to use another object or the global scope to find the
operation that I am after. Not a showstopper by any definition, but
irritating nonetheless.

-- 

Syver Enstad



More information about the Python-list mailing list