unpack sequence to tuple?

Moshe Zadka moshez at zadka.site.co.il
Tue Jan 23 10:31:24 EST 2001


On Tue, 23 Jan 2001 05:58:58 GMT, noahspurrier at my-deja.com wrote:

> 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)

No need for the tuple() or the () on the LHS:

cmd, host, user, key = sys.argv

Will work just ine.

If you want padding, just roll your own class:

class PaddedSeq:

	def __init__(self, s, n):
		self.s = s
		self.n = n

	def __len__(self):
		return self.n

	def __getitem__(self, i):
		if not (0<=i<n):
			raise IndexError
		try:
			return self.s[i]
		except IndexError:
			return None

cmd, host, something, something_else = PaddedSeq(sys.argv, 4)
	
-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!
Fingerprint: 4BD1 7705 EEC0 260A 7F21  4817 C7FC A636 46D0 1BD6




More information about the Python-list mailing list