Z
zillow20
Hi all,
I'm trying to understand generator functions and the yield keyword.
I'd like to understand why the following code isn't supposed to work.
(What I would have expected it to do is, for a variable number of
arguments composed of numbers, tuples of numbers, tuples of tuples,
etc., the function would give me the next number "in sequence")
####################################
def getNextScalar(*args):
for arg in args:
if ( isinstance(arg, tuple)):
getNextScalar(arg)
else:
yield arg
####################################
# here's an example that uses this function:
# creating a generator object:
g = getNextScalar(1, 2, (3,4))
g.next() # OK: returns 1
g.next() # OK: returns 2
g.next() # not OK: throws StopIteration error
####################################
I'm sure I'm making some unwarranted assumption somewhere, but I
haven't been able to figure it out yet (just started learning Python a
couple of days ago).
Any help will be appreciated
Akiel
I'm trying to understand generator functions and the yield keyword.
I'd like to understand why the following code isn't supposed to work.
(What I would have expected it to do is, for a variable number of
arguments composed of numbers, tuples of numbers, tuples of tuples,
etc., the function would give me the next number "in sequence")
####################################
def getNextScalar(*args):
for arg in args:
if ( isinstance(arg, tuple)):
getNextScalar(arg)
else:
yield arg
####################################
# here's an example that uses this function:
# creating a generator object:
g = getNextScalar(1, 2, (3,4))
g.next() # OK: returns 1
g.next() # OK: returns 2
g.next() # not OK: throws StopIteration error
####################################
I'm sure I'm making some unwarranted assumption somewhere, but I
haven't been able to figure it out yet (just started learning Python a
couple of days ago).
Any help will be appreciated
Akiel