I'm making my first step in object-oriented programming in python and I'm facing a problem that I don't understand.
I've a class "Annotation". Which represents an image of width 'w' and height 'h'.
class Annotation:
def __init__(self, data, width, height, point_coordinates=[]):
self.data = data
self.width = width
self.height = height
self.point_coordinates = point_coordinates
self.nb_point = len(point_coordinates)
I can add points at given coordinates (x,y) on my annotation thanks to the method 'add_point':
def add_point(self, x, y):
self.point_coordinates.append([x, y])
self.nb_point += 1
The problem is, in my main function I create 2 annotation objects of different sizes and I add a point to each of my annotations. After that I print the number of points of each of my objects, the result is 1 which is normal. But when I print the list of points of each of my objects the result is the same list...
if __name__ == '__main__':
ann = Annotation(np.full((512, 512), 127), 512, 512)
ann2 = Annotation(np.full((120, 120), 1), 120, 120)
ann.add_point(10, 20) #Add point to annotation 1
ann2.add_point(1, 1) #Add point to annotation 2
print("Number of point of ann: ",ann.get_nb_point()) #print (1)
print("Number of point of ann2: ", ann2.get_nb_point()) #print (1)
print("Point of ann: ", ann.get_point()) #print [[10, 20], [1, 1]]
print("Point of ann2: ", ann2.get_point()) #print [[10, 20], [1, 1]]
I really don't understand the problem here...
There is the full code of my class:
class Annotation:
def __init__(self, data, width, height, point_coordinates=[]):
self.data = data
self.width = width
self.height = height
self.point_coordinates = point_coordinates
self.nb_point = len(point_coordinates)
def get_point(self):
return self.point_coordinates
def get_nb_point(self):
return self.nb_point
def add_point(self, x, y):
self.point_coordinates.append([x, y])
self.nb_point += 1
if __name__ == '__main__':
ann = Annotation(np.full((512, 512), 127), 512, 512)
ann2 = Annotation(np.full((120, 120), 1), 120, 120)
ann.add_point(10, 20)
ann2.add_point(1, 1)
print("Number of point of ann: ",ann.get_nb_point()) #print (1)
print("Number of point of ann2: ", ann2.get_nb_point()) #print (1)
print("Point of ann: ", ann.get_point()) #print [[10, 20], [1, 1]]
print("Point of ann2: ", ann2.get_point()) #print [[10, 20], [1, 1]]