>Python doesn't have dynamic scoping.>2. Will it be possible in Python 3.0 to do the following:
>>def dotimes(n, callable):
for i in range(n): callable()
>>def block():
nonlocal i
for j in range(i):
print j,
dotimes seems ok and what is wrong with that function "block"? You do
not need to specify that i is "nonlocal", global i will be used.
for j in range(i):>>>i=10
def block():
print j,0 1 2 3 4 5 6 7 8 9>>>block()
... for i in range(n):>>def dotimes(n, callable):
... callable()
...
... for j in range(i):>>def block():
... print j,
...
... dotimes(5, block)>>def f():
...
Traceback (most recent call last):>>f()
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
File "<stdin>", line 3, in dotimes
File "<stdin>", line 2, in block
NameError: global name 'i' is not defined
The "nonlocal" keyword in Python 3 won't do this, either. It's for>>>
referencing names in outer lexical scopes, not outer dynamic scopes.
Jean-Paul