newbie questions

Erik Johnson spam at nospam.org
Fri Dec 10 22:17:35 EST 2004


> do yo have any idea of what is causing this problem?
> is it possible to do self.SortiesAnimeTitreLabel = [] to reset the var?
(it
> seems to work outside of the sub, but I believe that the var I'm erasing
is
> not the one I want but a local copy.


    Yeah.  I'm no Python guru, but I have a pretty good idea. Your intuition
is correct - you're killing a local copy. Functions (and methods) have their
own namespace. Variables declared within a function are local that that
function's name space, as well as formal variables that appear on the
function declartion (definition) line.

>>> z = 'z'
>>> def f(x):
...   print dir()
...   del x
...   print dir()
...
>>> z
'z'
>>> f(z)
['x']
[]
>>> z
'z'
>>>

    Secondly, within a class method, x and self.x are two different things.
The first is just a variable within the method namespace, the second is an
object attribute.  So, for your example, there probably is no reason to go
kill each list element individually (unless perhaps other code is accessing
that list NOT through the object.) Within an object method, if you honestly
don't want to kill the variable, you could just say:

self.SortiesAnimeTitreLabel = []

and that replaces that object's list with an empty one. Unless there are
other references to that same list, hte garbage collector will take it.
Other code accessing the list through this object's handle will see the new,
empty list, which I think is what you want in this case.

HTH,
-ej





More information about the Python-list mailing list