[Python-ideas] copyable iterators, named loops, continue a expression per indentation and a few functional things

Mathias Panzenböck grosser.meister.morti at gmx.net
Sat Dec 23 18:20:05 CET 2006

Copyable iterators

There are a few ways how you could implement this. However, it only makes sense for iterators, not
for generators!

Way 1:
Copyable iterators have a copy-method:
>>> L = [1,2,3,4]
>>> it1 = iter(L)
>>> it1.next()
>>> it2 = it1.copy()
>>> it2.next()
>>> it1.next()

Way 2:
Copyable iterators will be copied when you call it's __iter__-method:
>>> L = [1,2,3,4]
>>> it1 = iter(L)
>>> it1.next()
>>> it2 = iter(it1)
>>> it2.next()
>>> it1.next()

In way 1 you could check for copy ability per hasattr(it,"copy").

Named loops

With named loops you could break or continue other than only the inner most loop. I'm not sure about
a syntax, though.

for x in X as loop_a:
	for y in Y as loop_b:
		if cond1():
			break loop_a

		elif cond2():
			continue loop_a

		elif cond3():
			break # breaks loop_b

		elif cond4():
			continue # continues loop_b

Auto-curryfication for operators

I like haskells feature for curryfication of operators. I think this would be nice to have in python.

You could write instead of 'operator.add' the much shorter '(+)'.
Or even '(2+)' works.

>>> map((2+),[1,2,3,4])
>>> filter((>=2),[1,2,3,4])

__add__-method for functions

In haskell you can write 'f . g . h', which would be the same like '(\a -> f(g(h(a))))' (in python:
'lambda a: f(g(a))').
In python it would also be nice to have such a functionality. It could be implemented with a

Here is this functionality simulated with functors:

>>> class FunctionBase(object):
	def __add__(self,other):
		return lambda *args,**kwargs: self(other(*args,**kwargs))

>>> class F(FunctionBase):
	def __call__(self,s):
		return s+" foo"

>>> class G(FunctionBase):
	def __call__(self,s):
		return s+" bar"

>>> f = F()
>>> g = G()
>>> (f + g)("x")
'x bar foo'
>>> (g + f)("x")
'x foo bar'

What do you think?


More information about the Python-ideas mailing list