-4

I was getting confused by the purpose of "return" and "yield"

def countMoreThanOne():
    return (yy for yy in xrange(1,10,2))

def countMoreThanOne():
    yield (yy for yy in xrange(1,10,2))

What is the difference on the above function? Is it impossible to access the content inside the function using yield?

7
  • 2
    possible duplicate of Python: generator expression vs. yield Nov 19, 2014 at 11:50
  • 1
    in short - you are returning a generator of generator (with 1 element) in second function and a generator in first function.
    – Rusty
    Nov 19, 2014 at 11:52
  • what do you mean "Is it impossible to access the content inside the function using yield?" ?
    – Anentropic
    Nov 19, 2014 at 12:37
  • when you use the yield keyword in a function it becomes a generator function - when you call it you get a generator back. So by yielding a generator expression in the second example you've made a generator inside a generator wiki.python.org/moin/Generators
    – Anentropic
    Nov 19, 2014 at 12:43
  • @Anentropic Thanks for clearing the idea behind the second example, I was try to create a function which aim to use only generator then list for returning the result. Since I saw most of the example of using generator are come up with yield. Nov 19, 2014 at 13:57

2 Answers 2

4

In first you return a generator

from itertools import chain
def countMoreThanOne():
    return (yy for yy in xrange(1,10,2))

print list(countMoreThanOne())

>>> 
[1, 3, 5, 7, 9]

while in this you are yielding a generator so that a generator within the generator

def countMoreThanOne():
    yield (yy for yy in xrange(1,10,2))

print list(countMoreThanOne())
print list(chain.from_iterable(countMoreThanOne()))

 [<generator object <genexpr> at 0x7f0fd85c8f00>]
[1, 3, 5, 7, 9]

if you use list comprehension then difference can be clearly seen:-

in first:-

def countMoreThanOne():
    return [yy for yy in xrange(1,10,2)]
print countMoreThanOne()

>>> 
[1, 3, 5, 7, 9]

def countMoreThanOne1():
    yield [yy for yy in xrange(1,10,2)]
print countMoreThanOne1()

<generator object countMoreThanOne1 at 0x7fca33f70eb0>
>>>
10
  • I would add that the intention of the second function (i.e. a worthwhile use of yield) would be to change it to: def countMoreThanOne(): for yy in xrange(1,10,2): yield yy
    – Anentropic
    Nov 19, 2014 at 12:19
  • @Anentropic in this case just a generator will return only. Nov 19, 2014 at 12:24
  • yes, I was trying to demonstrate how to use the yield keyword in a useful way
    – Anentropic
    Nov 19, 2014 at 12:34
  • @Anentropic thanks for your suggestion. i was clearing the problem as it was asked. Nov 19, 2014 at 12:39
  • So in my second example, it is not a proper way of using "yield" to achieve the purpose of my function? Nov 19, 2014 at 13:52
0

After reading your other comments I think you should write the function like this:

def countMoreThanOne():
    return xrange(1, 10, 2)


>>> print countMoreThanOne()
xrange(1, 11, 2)
>>> print list(countMoreThanOne())
[1, 3, 5, 7, 9]

or even better, to have some point in making it a function:

def oddNumbersLessThan(stop):
    return xrange(1, stop, 2)


>>> print list(oddNumbersLessThan(15))
[1, 3, 5, 7, 9, 11, 13]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.