newbie questions

Adam DePrince adam at cognitcorp.com
Sat Dec 11 05:12:52 CET 2004


On Fri, 2004-12-10 at 22:17, Erik Johnson wrote:
> > 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

At risk of encouraging a "newbie" from partaking in the hideous and vile
vice of "programming by side effect" there is a way to make python do
almost what you want.

First, lets look at the "is" operator.  is is like ==, but instead of
answering the question "are these objects equivalent," it asks "are they
actually the same object."  

>>> a = []
>>> b = []
>>> a is b  # They are different objects
False
>>> a == b  # They have the sample value
True
>>> b = a   # Now we are assigning to b the same object as a.  
            # There are two references to it.
>>> a is b
True

Alright.  Now, as Erik pointed out if you assign to the variable the
computer will add that to the local name space.  This happens at
"compile" time (which is right after you hit enter twice at the CPython
command line.) 

For an example of this:

>>> a = 0
>>> def b():
...     print a
...
>>> def c():
...     print a
...     a = 1
...
>>> b()
0
>>> c()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in c
UnboundLocalError: local variable 'a' referenced before assignment

In b(), a was taken as being from the line above.  In c, it was from the
local name space.

So, how do we affect what you want?  Now promise me that you will never,
ever do this in code you consider yourself proud of.  Realize I'm
sharing this in the same spirit that kids in a drivers ed class might
view gory accident pictures.

Fortunately for you, lists are mutable.  Assigning to a auto-magically
makes it part of your local name space, hiding the global a.  But, if
you don't assign to a, you can still access it.  And because you can
access it you can mutate it.

>>> a = [1]
>>> def b():
...     a.append( 2 )
...
>>> def c():
...     a[0] = 0 # Yes, an assignment, but not to a
...
>>> def d():
...     a[:] = [] 
...
>>> b()
>>> a
[1, 2]
>>> c()
>>> a
[0, 2]
>>> d()
>>> a
[]
>>>

Now forgive me ... what you really want to do is follow Erik's advice.




Adam DePrince 





More information about the Python-list mailing list