unpack sequence to tuple?

noahspurrier at my-deja.com noahspurrier at my-deja.com
Tue Jan 23 00:58:58 EST 2001


I want to easily read arguments in a sequence into separate variables.
Basically, I want to use a Per1 idiom and do this:

    (cmd, host, user, key) = tuple(sys.argv)

The problem is that I cannot be sure how long sys.argv is.
If it's too short then tuple() fails with:

    ValueError: unpack tuple of wrong size

It would be cool if the extra tuple items just got set to None.
If the sequence is too long I would like the extra to be ignored
or stored into an "extra" tuple:

    (cmd, host, user, key, extra) = tuple(sys.argv)

This is not a big deal. It's certainly not unclear to simply read
each element, but I was hoping that there might be something short
and neat that would already do this:
    host = sys.argv[1]
    user = sys.argv[2]
    key = sys.argv[3]

Yours,
Noah




Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list