[exec cmd for cmd in cmds]

Schüle Daniel uval at rz.uni-karlsruhe.de
Wed Mar 8 12:30:02 CET 2006


Hello all,

 >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
 >>> lst = [p % (i,i,i) for i in range(10, 30)]
 >>> for item in lst:
...     exec item
...
 >>>
 >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
 >>> lst = [p % (i,i,i) for i in range(10, 30)]
 >>> [exec item for item in lst]
   File "<stdin>", line 1
     [exec item for item in lst]
         ^
SyntaxError: invalid syntax
 >>>

is this prohibited for some reasons or is this just happens to be
disallowed?


this is one more cool way
 >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
 >>> c = reduce(lambda x,y: x+y, [p % (i,i,i) for i in range(20,30)])
 >>> exec c

and one more :)
 >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
 >>> c = "".join([ p % (i,i,i) for i in range(20,30) ])
 >>> exec c

Regards, Daniel




More information about the Python-list mailing list