Implicit lists

Christian Tismer tismer at tismer.com
Sat Feb 1 23:18:47 EST 2003


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?

No solution so far was sufficient.
All the attempts to figure out what the user wants
have side effects, put implications or restrictions
on objects which aren't intuitive or are application
specific. I.E., At my taste, I would never accept an
array.array as a multiple argument, but others might.

Python's flexibility is great. At the same time, it is
its own enemy. I can have atomic objects, lists, tuples
and general sequences, and I can pass them to functions
which accept atoms, lists, tuples and sequences, but there is
no way to tell the function whether I meant to pass something
like an atomic object or like a sequence.

Is the consequence to always try to avoid polymorphic interfaces,
or do we need a new construct to express our intent?
I could imagine an inquiry function that would yield such information,
and in the case of strings, this would say "treat me as an atomic".

ciao - chris






More information about the Python-list mailing list