Showing posts with label python 3 examples and explanations. Show all posts
Showing posts with label python 3 examples and explanations. Show all posts

Thursday, January 5, 2012

Python 3 examples and Explanations: Generator Function


 Generator pattern are incredibly important and useful pattern  in python. It actually creates an iterator.

 Reference code is:

#this is a general function
def isprime(n);
            if n == 1:
                        return False
            for x in range(2,n):
                        if n % x == 0;
                                    return False
            else:
                        return True

# this is a generator function
def primes(n = 1):
            while(True):
                        if isprime(n): yield n
                        n += 1