I have some trouble with the following code:
from beat_class import Beat
# creates a list 'beats' and fills it with instances of the class 'Beat'
beats = []
for i in range(4):
beats.append(Beat())
# prints out the list attribute 'tones' of each 'Beat'-instance
for i in beats:
print(i.tones)
print()
# appends an int value to the first instance of the list 'beats'
beats[0].tones.append(3)
# prints out the 'tones'-attributes again
for i in beats:
print(i.tones)
print()
# compares the ids of the first 2 instances in the list 'beats' and prints
# 'True' if they are the same
print(id(beats[0].tones) == id(beats[1].tones))
Output:
[]
[]
[]
[]
[3]
[3]
[3]
[3]
True
The comments in the code above show what its different parts do.
My problem is that once I want to append a value to the list attribute tones of an instance of the class Beat inside the list beats, said attribute gets changed in all of the instances instead of just the one I wanted to.
Since I‘m relatively new to Python I‘m not quite sure how to fix this. I assume that this has something to do with the ids of the tones-attributes which appear to be all the same according to the console output of the last line of code above.
I have already tried out some stuff with the copy() and deepcopy()functions but none of it worked. :/
I would be really happy if someone could help me out. :)
__init__or sth forBeat? can you showBeatclass?Beatclass is presumably using a default[]value fortones. This means it will return the same empty list for all new instances that don't specify a value fortones, rather than a new, unshared empty list for each new instance as you'd like. It's a common mistake in Python.