Uplevel functionality

Maciej Sobczak no.spam at no.spam.com
Wed Dec 31 04:03:21 EST 2003


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 #???


Thank you very much,

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/





More information about the Python-list mailing list