There are several mistakes here:
First, you have inherited from "object" and there is no need to explicitly put it, you can leave it empty.
Second, the way you declared your variables in your class makes the class share the same values across all instances, thats why you get the latest modified values always. you should use "self.variable" instead, and declare a constructor function for that.
Third, you are modifying Test1.Dat1 4 times and appending the same object twice. thats why you get the same object every time.
this is the right way:
class TestDat(): # leave this empty
def __init__(self): # constructor function using self
self.Dat1 = None # variable using self.
self.Dat2 = None # variable using self
TestArray = [] #empty array
Test1 = TestDat() #this is an object
Test2 = TestDat() #this is another object
Test1.Dat1 = 0 #assigning value to object 1
Test1.Dat2 = 1 #assigning value to object 1
Test2.Dat1 = 3 #assigning value to object 2
Test2.Dat2 = 4 #assigning value to object 2
TestArray.append(Test1) #append object 1
TestArray.append(Test2) #append object 2
print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2
or even simpler:
class TestDat():
def __init__(self, Dat1, Dat2):
self.Dat1 = Dat1
self.Dat2 = Dat2
TestArray = [TestDat(0,1),
TestDat(3,4)]
print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2
or this way:
class TestDat():
def __init__(self):
self.Dat1 = None
self.Dat2 = None
TestArray = [] #empty array
size = 2 #number of loops
for x in range(size): # appending empty objects
TestArray.append(TestDat())
#initialize later
TestArray[0].Dat1 = 0
TestArray[0].Dat2 = 1
TestArray[1].Dat1 = 3
TestArray[1].Dat2 = 4
print("print everithing")
for x in range(len(TestArray)):
print("object "+str(x))
print(TestArray[x].Dat1)
print(TestArray[x].Dat2)
Answer from Carlos A. Rodriguez on Stack OverflowThere are several mistakes here:
First, you have inherited from "object" and there is no need to explicitly put it, you can leave it empty.
Second, the way you declared your variables in your class makes the class share the same values across all instances, thats why you get the latest modified values always. you should use "self.variable" instead, and declare a constructor function for that.
Third, you are modifying Test1.Dat1 4 times and appending the same object twice. thats why you get the same object every time.
this is the right way:
class TestDat(): # leave this empty
def __init__(self): # constructor function using self
self.Dat1 = None # variable using self.
self.Dat2 = None # variable using self
TestArray = [] #empty array
Test1 = TestDat() #this is an object
Test2 = TestDat() #this is another object
Test1.Dat1 = 0 #assigning value to object 1
Test1.Dat2 = 1 #assigning value to object 1
Test2.Dat1 = 3 #assigning value to object 2
Test2.Dat2 = 4 #assigning value to object 2
TestArray.append(Test1) #append object 1
TestArray.append(Test2) #append object 2
print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2
or even simpler:
class TestDat():
def __init__(self, Dat1, Dat2):
self.Dat1 = Dat1
self.Dat2 = Dat2
TestArray = [TestDat(0,1),
TestDat(3,4)]
print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2
or this way:
class TestDat():
def __init__(self):
self.Dat1 = None
self.Dat2 = None
TestArray = [] #empty array
size = 2 #number of loops
for x in range(size): # appending empty objects
TestArray.append(TestDat())
#initialize later
TestArray[0].Dat1 = 0
TestArray[0].Dat2 = 1
TestArray[1].Dat1 = 3
TestArray[1].Dat2 = 4
print("print everithing")
for x in range(len(TestArray)):
print("object "+str(x))
print(TestArray[x].Dat1)
print(TestArray[x].Dat2)
You're right, when you add objects it does add them by reference.
There's a couple ways to do this. Probably the cleanest is just to make a new object for each entry. If you absolutely need to use the same instances with changed values, you can use copy.copy:
from copy import copy
...
# Set up object
TestArray.append(copy(test1))
# Change stuff
TestArray.append(copy(test2))
See: https://docs.python.org/2/library/copy.html for the differences between copy (aka shallow copy) and deepcopy, as it may be important depending on the complexity of your object. It also tells you how to implement __copy__ and __deepcopy__ if copying the object is nontrivial.
So, TL;DR is I'd really suggest using new objects and discourage mutability, but copy is there if you need it.
2D NumPy array of objects vs. 2D Python list efficiency
How to Static Type Check an array of Class Object?
Searching Array of Data Objects
Python List vs Array
Videos
Say I have a class Book: somewhere and I have a function that takes in an array of Book objects as an argument. How do I Static Type check this?
I've tried
def find_max_author(book_objects: object[ ]):