Implicit lists
holger krekel
pyth at devel.trillke.net
Thu Jan 30 12:00:17 EST 2003
Alex Martelli wrote:
> holger krekel wrote:
> ...
> > hmmm. Maybe with a general-purpose
> >
> > def elems(*args):
> > l = []
> > for arg in args:
> > try: l.extend(arg)
> > except TypeError: l.append(arg)
> > return l
>
> Alas, doesn't meet specs...:
I presume you refer to this part
[dale]
In other words, I want to be able to treat an argument of arbitrary
type as a list of 1 if it isn't already a list.
which isn't an overly explicit specification. In particular,
it doesn't imply that strings are to be excluded.
But i agree that grouping into 'non-iterables except strings and
unicode' on one side and everything else on the other side most
often makes sense.
> [alex at lancelot mydata]$ python2.2
> Python 2.2.2 (#1, Oct 24 2002, 11:43:01)
> [GCC 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a=[]
> >>> a.extend('ciao')
> >>> a
> ['c', 'i', 'a', 'o']
> >>>
>
> basically, l.extend now accepts all iterables.
Sure. It's been like this since Python-2.1. Previously
it threw an TypeError.
ciao,
holger
More information about the Python-list
mailing list