-2

Assign a new value to the member variable of a class element in the list. Why do all the member variable values of the class elements in the list change? As you can see, I only changed the value of the member variable of Li [0] , but the value of the member variable of all elements in Li changed.enter image description here

How can I only change the single element in the list (example: li[0]) instead all of them?

1 Answers1

0

List multiplication just adds references to its existing values. A list has no way of knowing how its objects are constructed and certainly can't make new ones. You could try li =[A(2) for _ in range(3)] to construct your own.

tdelaney
  • 73,364
  • 6
  • 83
  • 116