Uplevel functionality
Levente Sandor
sandorlevi at yahoo.com
Wed Dec 31 10:45:34 EST 2003
More information about the Python-list mailing list
Wed Dec 31 10:45:34 EST 2003
- Previous message (by thread): Uplevel functionality
- Next message (by thread): Uplevel functionality
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Maciej Sobczak <no.spam at no.spam.com> wrote in message news:<bsu3bu$5rm$1 at nemesis.news.tpi.pl>... > Hello, > > I would like to know if there is any possibility in Python to execute > arbitrary scripts in the context higher in the stack frame. > > In essence, I would like to have the equivalent of the following Tcl code: > > proc repeat {n script} { > for {set i 0} {$i < $n} {incr i} { > uplevel $script > } > } > > This allows me to do: > > repeat 5 {puts "hello"} > > prints: > hello > hello > hello > hello > hello > > Or this: > > set i 10 > repeat 5 {incr i} > puts $i > > prints: > 15 > > That second example shows that the script provided as a second parameter > to the "repeat" procedure (the script is "incr i") is executed in the > context where the procedure was called, not locally in the procedure itself. > > The strongest analogy to the above repeat procedure in Tcl would be a > hypothetical Python function: > > def repeat(n, script): > for i in xrange(n): > EVALUATE script HIGHER IN THE STACK #??? > Yes, it would be def repeat(n, script): for i in xrange(n): exec(script, locals(), globals()) repeat(5, 'print "hello"') i = 10 repeat(5, 'i += 1') print i > > Thank you very much, With pleasure, ---- levi
- Previous message (by thread): Uplevel functionality
- Next message (by thread): Uplevel functionality
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list