Help me dig my way out of nested scoping
Ron_Adam
radam2 at tampabay.rr.com
Sun Apr 3 23:38:50 EDT 2005
On 3 Apr 2005 16:21:10 -0700, "Brendan" <spam4bsimons at yahoo.ca> wrote:
>Thanks for the tips. Making FW a callable class (choice 5) seems to be
>a good (if verbose) solution. I might just wrap my temporary values in
>a list [lastX, lastA, lastB] and mutate them as Michael suggests.
>Thanks to Michael especially for the explanation of the name-binding
>process that's at the heart of the issue.
>
>The other choicess are not as helpful to me for the following reasons:
>
>choice 1: I don't want the temporary values of lastA and lastB to be
>global variables in my case as they are great big numeric arrays, and
>I'd like their memory to be reclaimed after FW is done.
Generally global variables should be avoided in python if you are
doing a large application. For smaller ones, they are ok, but they
are just a little slower than local variables.
You could use a classic class which is a good way to store a single
group of data. The 'del' will unbind a name from an object so the
objects can be garbage collected.
class data:
A = []
B = []
def countupdown():
for n in xrange(11):
data.A.append(n)
data.B.append(10-n)
print data.A
print data.B
countupdown()
# store data # Check out pickle module for this.
del data
>choice 2: I tried this without success. Using Micheal's example, I
>would assume you mean something like this:
def outer():
def inner():
outer.b += 1
print outer.b
inner()
outer.b = 1 # <-- initialize here after function of same name
outer()
# save data method here
del outer # delete outer and it's attributes
More information about the Python-list
mailing list