Python yield a list with generator -


i getting confused purpose of "return" , "yield"

def countmorethanone():     return (yy yy in xrange(1,10,2))  def countmorethanone():     yield (yy yy in xrange(1,10,2)) 

what difference on above function? impossible access content inside function using yield?

in first return generator

from itertools import chain def countmorethanone():     return (yy yy in xrange(1,10,2))  print list(countmorethanone())  >>>  [1, 3, 5, 7, 9] 

while in yielding generator generator within generator

def countmorethanone():     yield (yy yy in xrange(1,10,2))  print list(countmorethanone()) print list(chain.from_iterable(countmorethanone()))   [<generator object <genexpr> @ 0x7f0fd85c8f00>] [1, 3, 5, 7, 9] 

if use list comprehension difference can seen:-

in first:-

def countmorethanone():     return [yy yy in xrange(1,10,2)] print countmorethanone()  >>>  [1, 3, 5, 7, 9]  def countmorethanone1():     yield [yy yy in xrange(1,10,2)] print countmorethanone1()  <generator object countmorethanone1 @ 0x7fca33f70eb0> >>> 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -