Conditional Expressions don't solve the problem

Guido van Rossum guido at python.org
Wed Oct 17 14:57:18 EDT 2001


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:

> >> Is iter just an extra object layer to map the interface. Will it be an
> >> overhead?
> >
> >What do you mean?
> 
> Well, I could knock up an iter of my own:
> 
> class iter:
> 	def __init__(self, function, endCase):
> 		self.endCase = endCase
> 		self.function = function
> 
> 	def __getitem__(self, v):
> 		r = self.function()
> 		if r == self.endCase:
> 			raise IndexError
> 		return r

(Technically, this is not an iterator but a pseudo-sequence.  An
iterator would have a next() method instead of __getitem__ and raise
StopIteration.)

> But this would add an extra layer of Python between a function and
> where it is used.
> 
> On our larger systems, I would prefer not to introduce such overheads
> where it is only a small coding convenience.
> 
> Is iter handled more effeciently than this?

Sure is -- here you are introducing an extra Python method call for
each item in the sequence; using iter(function, endCase) does the same
thing in C.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list