roulette01
roulette01

Reputation: 2472

Does setattr make a deepcopy?

https://docs.python.org/3/library/functions.html#setattr From this documentation, I cannot tell if a deep copy is made or not.

I'm working with a codebase currently that uses setattr is a rather disgusting way.

Here is the relevant snippet from the constructor of a class:

    for var in vars:
      # ExtMang is another class
      setattr(self, var, ExtMang(self))
    # create an alias
    for var in vars :
      if var.endswith("_modified") :
        setattr(self, var[:-9], getattr(self, var))

I'm mostly a C++ guy, so I don't know how common/acceptable this is to do in python, but this snippet looks awful to me.

Essentially, I'm trying to figure out if any unmodified var is a deepcopy or a reference to the modified var. e.g., say vars = ["price_modified"]. In the first loop, price_modified would be set as an attribute of self. In the second loop, it would set price as an attribute of self. I'm trying to figure out if self.price is a deepcopy or reference of self.price_modified

Upvotes: 0

Views: 27

Answers (0)

Related Questions