Exec with loops

Alex alex at somewhere.round.here
Tue Mar 28 18:30:15 EST 2000


> Anybody know how to use exec to run code containing arbitrarily
> complex loops? Thanks.

This'll probably do the trick:

>>> s = '''for i in 10 * [None]:
  print 'Hi there'
'''
... ... >>> 
>>> t = compile (s, 'string', 'exec')
>>> exec (t)
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
>>> 

Alex.



More information about the Python-list mailing list