====== Python yield ====== === Iterables === >>> list = [1,2,3,4,5,6] list is iterable >>> list = [x for x in range(7)] Iterables are handy but stores all values in memory === Generators === >>> listgenerator = (x for x in range(7)) >>> for i in listgenerator: print(i) Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly. === yield === Yield is a keyword that is used like return, except the function will return a generator. def createGenerator(): print("not running until next() called") for i in range(7): yield i*i x = createGenerator() for t in x: print(t) === Practical example === tail -f access.log import time def follow(thefile): thefile.seek(0,2) # Go to the end of the file while True: line = thefile.readline() if not line: time.sleep(0.1) # Sleep briefly continue yield line logfile = open("access-log") for line in follow(logfile): print(line) === Pipeline === def grep(pattern,lines): for line in lines: if pattern in line: yield line # Set up a processing pipe : tail -f | grep python logfile = open("access-log") loglines = follow(logfile) pylines = grep("python",loglines) # Pull results out of the processing pipeline for line in pylines: print line,