In C and variants, when you have something like this:
{
tmp1 <- 5
tmp2 <- 2
print(tmp1 + tmp2)
}
a 7 will be printed, but the tmp1
and tmp2
variables will be removed from stack once the scope }
ends. I sometimes want similar functionality in python so that I do not have to clean up (many) temporary variables after some point in time.
In R one can use local({insert piece of code})
for exactly this purpose.
How do we get similar but readable behavior in python?