unpack sequence to tuple?
Emile van Sebille
emile at fenx.com
Tue Jan 23 01:45:23 EST 2001
How about:
def unpack(cmd=None, host=None, user=None, key=None, *args):
return cmd, host, user, key
cmd, host, user, key = unpack(1,2)
print cmd, host, user, key
cmd, host, user, key = unpack(1,2,3,4,5)
print cmd, host, user, key
HTH,
--
Emile van Sebille
emile at fenx.com
-------------------
<noahspurrier at my-deja.com> wrote in message
news:94j6j1$3na$1 at nnrp1.deja.com...
> 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