Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: classes and list as parameter, whats wrong?

Reply
Thread Tools

Re: classes and list as parameter, whats wrong?

 
 
Dirk Zimmermann
Guest
Posts: n/a
 
      08-26-2005

Thanks for your help.

* James <(E-Mail Removed)> [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


 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-26-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


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
 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      08-26-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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding static methods : Whats wrong with these classes?? ankur Java 3 08-10-2008 08:28 PM
Tuple vs List: Whats the difference? Shafik Python 7 07-12-2007 06:12 PM
Getting linking error: not found in vtable (Trying to understand whats wrong the way I did my classes?) g35rider@gmail.com C++ 1 08-17-2006 05:10 PM
classes and list as parameter, whats wrong? Dirk Zimmermann Python 0 08-26-2005 09:34 AM
Whats wrong with this code? (nested classes question) Alfonso Morra C++ 7 07-19-2005 10:45 AM



Advertisments