Implicit lists

holger krekel pyth at devel.trillke.net
Mon Feb 3 03:28:55 EST 2003


Christian Tismer wrote:
> Erik Max Francis wrote:
> > Christian Tismer wrote:
> > 
> > 
> >>You are right, in general. The problem is simply that
> >>strings can be treated as objects or sequences all
> >>the time, and that a common error is to pass a string
> >>in a sequence context, and your function iterates over
> >>the single chars. This especially hurts when your
> >>function expects a list of filenames, and suddenly it
> >>creates a bunch of single char named files :-)
> > 
> > 
> > I don't see how you can eliminate strings' sequence interface without
> > either 1. breaking a huge amount of code, or 2. ending up with a string
> > presenting a very mixed and fragmented interface.  Individual characters
> > are routinely indexed from strings, the lengths of strings (via len) are
> > routinely retrieved, and slicing (really a special case of the sequence
> > protocol) is exceptionally common.
> 
> I don't see how your reply relates to my message.
> 
> > So how do you make the string type support all these behaviors but _not_
> > act as a standard sequence?  Why would you really want to?
> 
> I did not say that I want to remove the sequence protocol
> in this message. Maybe in a couple before, but nobody can
> hinder me to become smarter in a couple of days. :-)
> I was trying to spell the problem, but you are still stuck
> with implementation details, like sequence-ness.
> 
> This is most probably not the distinguishing property that
> we are searching for, so please drop that idea that I want
> to remove the sequence protocol for strings. This has not
> been in my message.
> 
> The summary is this:
> How can I express that I want to pass a string or some other
> object as a single parameter to a function that both accepts
> singletons and sequences? How can I prevend that Joe User
> ignores my interface and passes a string, and I misinterpret
> it as a bunch of characters?

I'd call upon 

    "In the face of ambiguity, refuse the temptation to guess."
and 
    "Explicit is better than implicit"

So I'd still propose to do

    def myfunc(args, noiter=(str, unicode)):
        ...
        if isinstance(args, noiter):
            # don't iterate 
        ...

this cures exactly the problem that the string types
in python suffer from: you often want them as an atom 
and not iterable 99% of the time.   If somebody
makes his own string type, then usually they 
either inherit from str/unicode or completely
make up their own class/type and probably won't even
implement a character-iteration.  And if they do then
they have to make this explicit to 'myfunc'.  If that
'myfunc' is deep inside a module (not directly accessed
from the outside) then we can make 'noiter' a module
global so that users can append their class to it.

The above is also the way it is done in the standard lib
anyway. 

cheers,

    holger





More information about the Python-list mailing list