Are generators in nested functions possible?
Bjorn Pettersen
BPettersen at NAREX.com
Thu Jun 20 19:13:10 EDT 2002
I've got the following code to generate the indicies of all nearest
neighbours (span levels) in a multi dimensional qube:
def generateIndexes(length=5, span=1):
spanvalues = range(-span, span+1)
def genInd(res, length):
if length == 1:
for val in spanvalues:
yield res + [val]
else:
for i in range(length):
for val in spanvalues:
genInd(res + [val], length-1)
return genInd([], length)
However, when I try to use it it doesn't seem to return anything:
>>> for i in generateIndexes(3):
... print i
...
>>>
I tried getting the iterator (or is it generator?) directly and calling
next() on it, and it seems like it raises a StopIteration exception
immediately:
>>> g = generateIndexes(3)
>>> g.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
>>>
Can anyone help me understand what is going on here?
-- bjorn
More information about the Python-list
mailing list