Partially unpacking a sequence

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 17:40:39 EDT 2006


Paul McGuire wrote:

> Forgot one:
>
>  _,_,a,_,b,_,_,_ = y
>
> There actually is some merit to this form.  If the structure of y changes
> sometime in the future (specifically if a field is added or removed), this
> statement will fail noisily, calling your attention to this change.  But if
> a new field is added, say at the front of the list, the previous methods
> will all silently succeed, but now giving you the values formerly known as
> y[1] and y[3].

if this is likely to happen, an straightforward assert is a lot easier to parse than
a one-two-a-three-oops-one-two-a-four-five-etc number of underscores:

    assert len(y) == 8
    a = y[2]
    b = y[4]

</F>






More information about the Python-list mailing list