Q:

Generating an infinite list?

Knowing this will most likely end with a failure, I want to know how Python will treat such a scenario and whether or not my code actually "makes sense", at least in principle.
So here's my attempt, I'm pretty new to python (a few days ago), so I'm sure my code seems funny to those of you with more experience, but please bear with me.      
def make_inf_lst(lst):
  lst = [0]
  while 1 == 1:
    for i in range(0, len(lst)):
      add = lst[i] + 1
      lst.append(add)
print(make_inf_lst([]))

This is the error I got in my DOS window trying to run the above: 
Traceback (most recent call last):
  File "break_python.py", line 7, in <module>
    print(make_inf_lst([]))
  File "break_python.py", line 6, in make_inf_lst
    lst.append(add)
MemoryError

Thank you in advance.

A:

Languages with "infinite" data structures are actually producing objects that produce their values lazily, or "on-demand". Rather than creating the entire list up front, they create an object that, when asked for a value, can create it at that time.
In Python, this is most easily managed using a generator.
def make_inf_sequence():
    x = 0
    while True:
        yield x
        x = x + 1

# This returns immediately
naturals = make_inf_sequence()

# This loop runs forever
for i in naturals:
    print(i)

