Executing a string as python code
Steve Holden
sholden at holdenweb.com
Wed Oct 24 08:20:17 EDT 2001
More information about the Python-list mailing list
Wed Oct 24 08:20:17 EDT 2001
- Previous message (by thread): Executing a string as python code
- Next message (by thread): Executing a string as python code
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Stephen Boulet" <stephen at theboulets.net> wrote in message news:ttdbclaabhte92 at corp.supernews.com... > Yes, but this is no cgi script. It's in my program, which, after all, also > executes python code. > > If I write: > > << > command = '' > for i in range(100): > command = command + 'list%d = []\n' % i > > exec(command) > > >> > > That's no less secure than typing them out by hand, right? > This may be so. But your next problem is then presumably to write a loop to process each of these different variables. The major point (assuming no user input is associated with your executed code) is the pollution of your name space in a way which doesn't help you in your overall programming goal. You haven't yet demonstrated why you'd rather run your code than something like lst = [] for i in range(100): lst.append([]) If you really need 100 different empty lists, why not create a list, or dictionary, of lists to hold them? Saving each one as a variable makes them difficult to deal with, which is quite unnecessary. Your way you have to create a statement referencing listN and then execute it. stmt = "list%d.append(something)" % n exec(stmt) With a list of lists you can just execute lst[n].append(something) Simpler, cleanr, faster. Where are the *dis*advantages? regards Steve -- http://www.holdenweb.com/
- Previous message (by thread): Executing a string as python code
- Next message (by thread): Executing a string as python code
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list