0

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. :)

2
  • 3
    do you by chance have a list as a default argument to __init__ or sth for Beat? can you show Beat class? Commented Aug 5, 2022 at 12:08
  • 1
    Your Beat class is presumably using a default [] value for tones. This means it will return the same empty list for all new instances that don't specify a value for tones, rather than a new, unshared empty list for each new instance as you'd like. It's a common mistake in Python. Commented Aug 5, 2022 at 12:10

1 Answer 1

1

Does your Beats __init__ look something like:

class Beat:
   def __init__(self, tones=[]):
       self.tones = tones

If so, the problem is that tones defaults to the same list (actually the same list in memory). What you want is something like:

class Beat:
   def __init__(self, tones=None):
       self.tones = tones or []

That way you'll have a separate list for each Beat instance.

See more info on this here

Sign up to request clarification or add additional context in comments.

1 Comment

@Truff2107 Please see What to do when someone answers my question? and How does accepting an answer work?, this will help future readers and also allow to mark other questions as duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.