I found this code in the wild (simplified here), and I was wondering whether this is considered a good practice or not, that is, making a shallow copy (of an object’s attribute in this case) for the sole purpose of updating the attribute (and reduce verbosity?). I ask because it is not the first time I see this kind of pattern and it bothers me a bit.

class DrawingBoard:
    def __init__(self):
        self.raw_points = []

    def add_point(self, x, y):
        Point = collections.namedtuple('Point', ['x', 'y'])
        points = self.raw_points
        points.append(Point(x, y))
        if len(points) > ......:
           # DO SOMETHING

board = DrawingBoard()
for x in np.arange(0, 10, 0.1):
    board.add_point(x, np.sin(x))

For context, the actual class is performing some action when a certain amount of points have been added. On my first read, I miss this update was happening (the code is a fair bit more complex).

My question is: Is it just me not used to this pattern, and this is a mere question of less verbosity vs clarity? or is there something else I am missing? I can see that if we were updating multiple attributes, it could get quite verbose, but I find calling self.raw_points.append(Points(x,y)) instead of points.append(Points(x,y))so much clearer.

New contributor

Joca is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.