Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

Is it pythonic to use a shallow copy to update an object's attribute?

$
0
0

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.


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>