Re: [Tutor] How to control list input ?

Magnus Lycka magnus at thinkware.se
Thu Jun 3 15:08:38 EDT 2004


> For my purposes doctlist can't be just any arbitrary list of lists; it
> will have to be made of a precise number of elements, that musn't be
> hardcoded but definable; 

So it's not a list!

> how do you suggest I try coding a) a way in
> which to define what the structure of my list ought to be like and b) a
> way of checking the input to make sure it corresponds ? 

Don't use a list. Use a class. If you want to be able to access attributes
in your instance objects as if they were list members, just implement the
__getitem__ method.

>>> class ListLike:
	def __init__(self, name, address, phone):
		self.name=name
		self.address=address
		self.phone=phone
	def __getitem__(self, i):
		return [self.name, self.address, self.phone][i]

	
>>> a = ListLike('Santa Claus', 'North Pole', 123)
>>> for i in a:
	print i

	
Santa Claus
North Pole
123


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list