Hi, I think I don’t understand something fundamental about classes and objects. I’m trying to create two instances of a class and then change an attribute of one of those instances, but when I try a corresponding attribute in the other instance also changes:
#Testing object
class Test:
name=''
list=[]
def __init__(self,name):
self.name=name
def change(self,num):
self.list.append(num)
#Creating two instances of test
a=Test('qqqq')
b=Test('wwww')
#Changing list attribute of instance a
a.change('2')
#list attributes of both instances change
print(a.name)
print(a.list) #expected [2] and I get [2]
print(b.name)
print(b.list) #expected [] but I get [2]
The expected behaviour is:
- print(a.list)==>“[2]”
- print(a.list)==>“[ ]”
but what I get is: - print(a.list)==>“[2]”
- print(a.list)==>“[2]”
It’s a very basic example, so I can’t figure out what could be going wrong here.