Oct-03-2018, 09:29 PM
I am pretty new to python and I have a question regarding best practices that I didn't find an answer for. The problem is simple: you have a class that should have a method to update an object of such class. My question is if it is better to make a copy of that object or modify it in place. For example most pandas methods allow both and you can select using argument 'inplace'. As I see it, the options are following:
from copy import copy class Foo: def __init__(self, x): self.x = x def update_only(self, x): self.x = x def update_and_return(self, x): self.x = x return self def copy_and_update(self, x): new = copy(self) self.x = x return new def create_updated(self, x): new = self.__class__(x) return newWhat method would you use in your project? My concerns are mostly about testing and maintaining the code. Please share anything: ideas, experience, links to articles, links to github projects with examples, ...