Implicit lists
Christian Tismer
tismer at tismer.com
Fri Jan 31 09:46:16 EST 2003
Alex Martelli wrote:
> On Friday 31 January 2003 04:36 am, Christian Tismer wrote:
> ...
>
>>Besides th fact that I believe strings as sequences
>>should be deprecated, since nobody makes use of it,
>
>
> This is an overbid, IMHO: some (small) fraction of the time,
> say 10%, I'm quite happy that strings are sequences for
> looping purposes; and far more often than that, for purposes
> of slicing, concatenation, repetition. Deprecation can maybe
> be mooted for Python 3.0, of course, but no earlier.
Sorry, I didn't really express what I meant.
A reply from Guido first reminded me that
we of course need string slicing and indexing.
And then I realized that we are not seeking
for "sequenceness" or "stringness" at all.
What people really want to know is whether
"is this argument meant as a single argument
or is it a container of arguments".
That turns the overall question into
"is something a container"?
This still gives some ambiguities with multi-
purpose chamaeleans like the Numeric arrays,
but strings and array.array objects are very well
covered: They *are* sequences, but you don't
want them to be taken as argument collections,
since they are no containers.
Here the same, written in some bad Python style,
but as a builtin, it would do quite much
simplification for argument checking:
>>> def iscontainer(obj):
... try:
... obj[0:0] # is sequence
... try:
... buffer(obj)
... return False
... except:
... pass
... return True
... except:
... return False
...
>>> iscontainer("")
0
>>> iscontainer(1)
0
>>> iscontainer(())
1
I'm (ab)using the fact that sequences which are
no containers usually support the buffer interface.
ciao - chris
More information about the Python-list
mailing list