classes and list as parameter, whats wrong?

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 26 12:36:38 EDT 2005


Dirk Zimmermann wrote:
> But still, it is not absolutely clear for me, what is going on. So, at
> least just for my understanding: The parameter LL is created just once
> for the whole class and not for the object (because I del the object
> explicitly, which should destroy the object)?
del does nothing but remove one binding early.
As far as effect on the underlying object,
     del v
and
     v = None
have the same effect.

<original main>:
 >    def main():
 >        l1 = ['a', 'b', 'c']
 >        lNames = ['n1', 'n2', 'n3']
 >        for name in lNames:
 >            objC = cClass()
 >            for each in l1:
 >                objC.addFile(each)
 >            print objC.list
 >            del objC
The del in main is superfluous.  For all but the last iteration,
the objC = c.cClass() will dereference the previous objC, and
the final trip through the loop ends up by exiting the function
which will have a similar effect.

An experiment which will show this:
     import sys
     q = r = object()
     print sys.getrefcount(q),
     del r
     print sys.getrefcount(q),
     r = q
     print sys.getrefcount(q),
     r = None
     print sys.getrefcount(q)

Note that whenever you call sys.getrefcount, the argument to the
function itself will increase the count by 1.  This demonstrates that:

     print sys.getrefcount(object())


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list