Re: classes and list as parameter, whats wrong?

Discussion in 'Python' started by Dirk Zimmermann, Aug 26, 2005.

  1. Thanks for your help.

    * James <> [2005-08-26 11:42]:
    > No, the default paramter LL is only ever created once, not
    > reinitialised every time the constructor is called - this is quite a
    > common gotcha!


    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)?

    Thanks,
    Dirk
     
    Dirk Zimmermann, Aug 26, 2005
    #1
    1. Advertising

  2. 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


    Yes. And because a lists are mutable, you can alter that one instance of
    the list - and teh following instances of your class ill see the
    changes. This is really a common mistake.

    Diez
     
    Diez B. Roggisch, Aug 26, 2005
    #2
    1. Advertising

  3. 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 David Daniels, Aug 26, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Martins

    Whats wrong with this provider ??

    Alexandre Martins, Aug 14, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    363
    Guenther Liebowitz
    Aug 14, 2003
  2. Dirk Zimmermann

    classes and list as parameter, whats wrong?

    Dirk Zimmermann, Aug 26, 2005, in forum: Python
    Replies:
    0
    Views:
    259
    Dirk Zimmermann
    Aug 26, 2005
  3. Alfonso Morra
    Replies:
    7
    Views:
    321
    Alfonso Morra
    Jul 19, 2005
  4. Seb
    Replies:
    2
    Views:
    326
  5. ankur
    Replies:
    3
    Views:
    353
    ankur
    Aug 10, 2008
Loading...

Share This Page