N
Nils Grimsmo
hi,
i'm having some trouble nesting functions. consider the following:
def h():
x = 1
def g():
print x # ok, x is taken from h
g()
def f():
x = 1
def g():
print x # this is not ok
x = 2 # this implies that x is local to g
g()
h() # ok
f() # UnboundLocalError: local variable 'x' referenced before assignment
when i run this code i get:
1
Traceback (most recent call last):
File "test.py", line 15, in ?
f() # UnboundLocalError: local variable 'x' referenced before assignment
File "test.py", line 12, in f
g()
File "test.py", line 10, in g
print x # this is not ok
UnboundLocalError: local variable 'x' referenced before assignment
how do i declare that x belongs to the parent function, so that i can do
assignments to it? do i have to put it into a compound object?
it would be very handy to be able to do this if i have nested functions
that use a lot of varables. only passing the variables you assign to as
packed compound parameters is a bit ugly, since what subset of all
variables this is might change.
i cannot say i like the python scope rules yet. they probably are
practical, but they seem complicated an unstructured to me.
take for example
class C:
y = 0
def f(self):
print y
which does not work unless y is a global. i understand and agree too why
y is not taken from the instance (self), because of the way classes and
instances relate in python. what i do not understand, is why y is not
taken from C, but from global, when nothing is specified. in the
previous example, with the functions h() and g(), g() took x from h().
why should not f() take y from C?
klem fra nils
i'm having some trouble nesting functions. consider the following:
def h():
x = 1
def g():
print x # ok, x is taken from h
g()
def f():
x = 1
def g():
print x # this is not ok
x = 2 # this implies that x is local to g
g()
h() # ok
f() # UnboundLocalError: local variable 'x' referenced before assignment
when i run this code i get:
1
Traceback (most recent call last):
File "test.py", line 15, in ?
f() # UnboundLocalError: local variable 'x' referenced before assignment
File "test.py", line 12, in f
g()
File "test.py", line 10, in g
print x # this is not ok
UnboundLocalError: local variable 'x' referenced before assignment
how do i declare that x belongs to the parent function, so that i can do
assignments to it? do i have to put it into a compound object?
it would be very handy to be able to do this if i have nested functions
that use a lot of varables. only passing the variables you assign to as
packed compound parameters is a bit ugly, since what subset of all
variables this is might change.
i cannot say i like the python scope rules yet. they probably are
practical, but they seem complicated an unstructured to me.
take for example
class C:
y = 0
def f(self):
print y
which does not work unless y is a global. i understand and agree too why
y is not taken from the instance (self), because of the way classes and
instances relate in python. what i do not understand, is why y is not
taken from C, but from global, when nothing is specified. in the
previous example, with the functions h() and g(), g() took x from h().
why should not f() take y from C?
klem fra nils