I got a strange behavior with python class, that doesn't seem normal to me, could you take time to help me on that ?
The code has been executed under python 3.9 and 3.6.
I got multiple instances from the same class:
class Obj:
txt_list = []
txt_dictionnary = {}
txt_var = ""
def __init__(self, txt):
self.txt_list.append(txt)
self.txt_dictionnary["hello"] = txt
self.txt_var = txt
Main code :
if __name__ == '__main__':
Obj1 = Obj("1")
print(f"txt_list1 = {Obj1.txt_list} - txt_dictionnary1 = {Obj1.txt_dictionnary} - txt_var1 = {Obj1.txt_var}")
Obj2 = Obj("2")
print(f"txt_list1 = {Obj1.txt_list} - txt_dictionnary1 = {Obj1.txt_dictionnary} - txt_var1 = {Obj1.txt_var}")
Obj2.txt_list.append("3")
print(f"txt_list1 = {Obj1.txt_list} - txt_dictionnary1 = {Obj1.txt_dictionnary} - txt_var1 = {Obj1.txt_var}")
Output :
txt_list1 = ['1'] - txt_dictionnary1 = {'hello': '1'} - txt_var1 = 1
txt_list1 = ['1', '2'] - txt_dictionnary1 = {'hello': '2'} - txt_var1 = 1
txt_list1 = ['1', '2', '3'] - txt_dictionnary1 = {'hello': '2'} - txt_var1 = 1
For me Obj1 shouldn't be changed when I instantiate and modify the Obj2 since Obj1 and Obj2.
We also see, only the simple variable don't change on modification/instantiation.
Did I forget something ?