I just learnt about generators in Python a week back. From what I understood, the ‘yield’ returns a generator object instead of the, say, an entire array as is.
Here is the code I wrote for getting the digits of an integer:
def getDigits(m):
for d in str(m):
yield int(m)
This should return the digits of the integer passed to it as a generator object.
But when I do:
for i in getDigits(123):
print i
I get the output as:
123
123
123
instead of:
1
2
3
What is going on? Am I doing something wrong?
It should be
yield int(d)instead ofyield int(m):