ram <[EMAIL PROTECTED]> wrote: > Here's a little issue I run into more than I like: I often need to > unpack a sequence that may be too short or too long into a fixed-size > set of items: > > a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2) > > What I usually do is something like this: > > a, b, c = (list(seq) + [None, None, None])[:3] > > but that just feels rather ugly to me -- is there a good Pythonic > idiom for this?
Pythonic might be to be explicit: i.e. know in advance how long the sequence actually is. One drawback I see with your code is that it doesn't give you any way to specify different defaults for the values. So here's an alternative to consider: try passing your sequence to a function. This lets you specify appropriate defaults, and it reads quite cleanly. Of course it also forces you to extract the code using those variables out into a separate function, but that may not be a bad thing. >>> def process(a=None, b=None, c=None): print a, b, c >>> seq = iter('abcd') >>> process(*itertools.islice(seq,0,3)) a b c >>> seq = iter('ab') >>> process(*itertools.islice(seq,0,3)) a b None >>> -- http://mail.python.org/mailman/listinfo/python-list