In Python, a 'Generator' is a specific type of function that allows you to declare a function that behaves like an iterator, enabling you to iterate over it using a loop, just as you could with a list.
Consider the following example. In order to generate a sequence of numbers from 0 to N, you could use a simple for-loop:
def firstN(n):
num, nums = 0, []
while num < n:
nums.append(num)
num += 1
return nums
print(firstN(10))
This function, however, creates an entire list in memory. If you wanted to generate a very large list of numbers, it could potentially consume significant amounts of memory and slow down your program.
Now, consider the equivalent generator:
def firstN(n):
num = 0
while num < n:
yield num
num += 1
for i in firstN(10):
print(i)
The generator function looks like a normal function, except for the use of the 'yield' keyword. Yet, it doesn't compute all values at once - it only generates them on-the-fly one at a time.
Python Generators thus offer a powerful tool in Python - particularly useful when working with large streams of data that require on-the-fly computation, where memory efficiency is a concern.