Implicit lists

Thanos Vassilakis tvassila at siac.com
Fri Jan 31 10:16:14 EST 2003


A long time ago I wrote a

class IsLike:
     def  __init__(self, *likeWhat):
          self.likeWhat = likeWhat

     def like(self, other):
          "maybe  overridde this"
          return type(other) in self.likeWhat

So you could do

ListLike = IsLike(list, tuple)

from mytypes import ListLike

lst = maybeAnArg
if not ListLike.like(lst):
    lst = [lst]
for x in lst:
    whatever


thanos



                                                                                                                                
                    Christian Tismer                                                                                            
                    <tismer at tismer.com        To:     Alex Martelli <aleax at aleax.it>                                            
                    >                         cc:     holger krekel <pyth at devel.trillke.net>, python-list at python.org            
                    Sent by:                  Subject:     Re: Implicit lists                                                   
                    python-list-admin@                                                                                          
                    python.org                                                                                                  
                                                                                                                                
                                                                                                                                
                    01/31/2003 09:46                                                                                            
                    AM                                                                                                          
                                                                                                                                
                                                                                                                                




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


--
http://mail.python.org/mailman/listinfo/python-list









More information about the Python-list mailing list