Struggling with python generator function -
Struggling with python generator function -
i've checked out several yield examples, cannot figure out how i'm supposed utilize in task.
i have create generator function has give 1 word read file when called (and next 1 , on).
- pass file path function - read in word char char , assign variable - ........ yield word
the thing want know is, how generator function yield 1 word , know next word. i'm not looking ready-made solutions, i'd understand what's going on.
your generator function work non-generator function adds words list. except instead of adding them list, yield word.
so function adds them list be:
def per_word(filename): result = [] open(filename) fh: line in fh: word in line.split(): result.append(word) homecoming result
the generator function yields instead:
def per_word(filename): open(filename) fh: line in fh: word in line.split(): yield word
python generator
Comments
Post a Comment