Re: classes and list as parameter, whats wrong?

2005-08-27 Thread Dirk Zimmermann
Thanks to everyone! I think I understand the things behind this
behaviour, now.

And as the life goes: Today I found the description of my "problem" in
the Python Tutorial! I think I should read it more often, more
carefully:-)


Best,
Dirk

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes and list as parameter, whats wrong?

2005-08-26 Thread Terry Reedy

"Dirk Zimmermann" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> 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)?

Default parameter values are created (once) for the function, not for the 
class or instance.  Function default parameter expressions are evaluated 
once and only once when the function is defined.  Code that you want 
evaluated with each call goes in the function body.

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes and list as parameter, whats wrong?

2005-08-26 Thread Scott David Daniels
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.

:
 >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
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes and list as parameter, whats wrong?

2005-08-26 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes and list as parameter, whats wrong?

2005-08-26 Thread Dirk Zimmermann

Thanks for your help.

* James <[EMAIL PROTECTED]> [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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes and list as parameter, whats wrong?

2005-08-26 Thread James
No, the default paramter LL is only ever created once, not
reinitialised every time the constructor is called - this is quite a
common gotcha! You want to do something like:

 class cClass:
 """ Base class to handle playlists, i.e. the files, the name, etc. """
 def __init__(self, LL=None):
 if LL == None:
 LL = []
 self.list=LL
 def addFile(self,L):
 self.list.append(L)

James

On 8/26/05, Dirk Zimmermann <[EMAIL PROTECTED]> wrote:
> Hi!
> 
> I have a problem in a program. And I don't understand what is going on.
> I can code something, that the "error" doesn't occur anymore. But I
> still don't know the reason and this is unsatisfactory: Did I understood
> something wrong or is there a bug? To make it short, I boiled down the
> program to the crucial part.  It looks like this:
> 
> --- START 
> #!/usr/bin/env python
> 
> class cClass:
> """ Base class to handle playlists, i.e. the files, the name, etc. """
> def __init__(self, LL=[]):
> self.list=LL
> def addFile(self,L):
> self.list.append(L)
> 
> 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
> 
> if __name__ == "__main__":
> main()
> --- END 
> 
> If I start it, I get the following output:
> 
> ['a', 'b', 'c']
> ['a', 'b', 'c', 'a', 'b', 'c']
> ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
> 
> 
> Why does this list grow? I thought, with every new instance the list LL
> is set to []?
> 
> How can bring light in this?
> 
> 
> Best,
> Dirk
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


classes and list as parameter, whats wrong?

2005-08-26 Thread Dirk Zimmermann
Hi!

I have a problem in a program. And I don't understand what is going on.
I can code something, that the "error" doesn't occur anymore. But I
still don't know the reason and this is unsatisfactory: Did I understood
something wrong or is there a bug? To make it short, I boiled down the
program to the crucial part.  It looks like this:

--- START 
#!/usr/bin/env python

class cClass:
""" Base class to handle playlists, i.e. the files, the name, etc. """
def __init__(self, LL=[]):
self.list=LL
def addFile(self,L):
self.list.append(L)

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

if __name__ == "__main__":
main()
--- END 

If I start it, I get the following output:

['a', 'b', 'c']
['a', 'b', 'c', 'a', 'b', 'c']
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']


Why does this list grow? I thought, with every new instance the list LL
is set to []?

How can bring light in this?


Best,
Dirk


-- 
http://mail.python.org/mailman/listinfo/python-list