I had been writing this code, so I expected that function from class, has been applied to the first and second variable from the list.
Imported files contains: ch0.txt = 111, ch1.txt = 123
Yet, only the ch1.txt is loaded, and it is set for all of variable from the list (sic!).
Could you explain me why indexing like this does not work, or how I can change code to load content of each file to another variable; in way: s[i] = content of file ch[i].txt
class Test:
name = "empty"
def load(self, file_name):
wczytanie = open(file_name, "r")
dane = wczytanie.read().split("\n")
self.name = dane[0]
pos = Test()
s = [pos, pos, pos]
print(s)
s[0].load("ch" + str(0) + ".txt")
s[1].load("ch" + str(1) + ".txt")
print(s)
print(s[0].name)
print(s[1].name)
The output:
[<__main__.Test object at 0x00000230771A40D0>, <__main__.Test object at 0x00000230771A40D0>, <__main__.Test object at 0x00000230771A40D0>]
[<__main__.Test object at 0x00000230771A40D0>, <__main__.Test object at 0x00000230771A40D0>, <__main__.Test object at 0x00000230771A40D0>]
123
123
Process finished with exit code 0