Implicit lists

Chad Netzer cnetzer at mail.arc.nasa.gov
Fri Jan 31 05:11:54 CET 2003


On Thu, 2003-01-30 at 19:28, Christian Tismer wrote:

> In my opinion, the crux of this all is the decision
> that strings are iterable.

Ah, but I work with Numeric arrays a lot, which are also iterable, and
which I like to package into tuple or lists for various reasons.  These
same problems can crop up as well (must be careful to pass a sequence of
arrays, rather than just an array, etc).  So it isn't just strings that
present a problem.

Using variable parameters, you could build an interface that will accept
single arguments (ie. foo(x) ), and would accept iterable objects or
sequences as long as you explicitly specified that (ie. foo(*seq) ),
while rejecting single arg sequences.  A bit ugly, but could be useful
with lots of string/list/tuple mixes.

so,

def foo( *args ):
    for a in args: print a,

a_string = "baked beans"
a_list_of_strings = [ "spam", "spam", "spam", "spam", "baked beans",
"spam" ]

foo( a_string )  # okay
foo( *a_list_of_strings )  #okay
foo( a_list_of_strings )  # could be made to NOT be okay

Messy, but it forces the user to consider the difference between using a
single arg, or using a list of arguments.

Anyway, it is just an (possibly lack-of-sleep-induced) idea.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list