Why is Python popular, while Lisp and Scheme aren't?
Chad Netzer
cnetzer at mail.arc.nasa.gov
Wed Nov 27 20:03:11 EST 2002
On Wednesday 27 November 2002 15:02, holger krekel wrote:
> Michael Hudson wrote:
> > For what it's worth (not much) I'm forever writing
> >
> > for k, v in d:
> > ....
> >
> > I can see the arguments for consistency, but I think this is my most
> > reliable Python error at the moment.
>
> TypeError: unpack non-sequence
>
> is also my number one "basic" error i am producing.
The way it works just seems natural to me:
Python 2.2.2 (#1, Nov 21 2002, 08:18:14)
>>> d = [ (1,2), (3,4) ]
>>> for x,y in d:
... print "x,y: ", x, y
...
x,y: 1 2
x,y: 3 4
Given that we can do:
x,y = (1,2) # Or x,y = 1,2
but not
x,y = (1,2,3)
it all seems consistent and aboveboard (and would be strange and awkward if
things didn't work this way).
I suppose one gotcha is that I might have expected the following to work:
e = [1,2]
for x,y in e:
print x,y
just because e is length two (but that would be a fragile case to rely upon).
I suggest creating your own 'unflatten' function, and just learn to start
using it. Anyone with a quick and dirty implementation?
ie.
a = [1,2,3,4,5,6]
unflatten(a, 2) # should return [(1,2), (3,4), (5,6)]
unflatten(a, 3) # should return [(1,2,3), (4,5,6)]
I imagine it is trivial with list comprehensions, but I haven't really
bothered to learn them yet (and I'm leaving for the holidays in about 20
seconds :)
--
Bay Area Python Interest Group - http://www.baypiggies.net/
Chad Netzer
cnetzer at mail.arc.nasa.gov
More information about the Python-list
mailing list