classes and list as parameter, whats wrong?

D

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top