I have two classes. They’re almost identical, except for 2 attributes. I need to copy all the attributes over from one to the other, and I’m just wondering if there is a pattern or best practice, or if I should just basically do:
spam.attribute_one = foo.attribute_one
spam.attribute_two = foo.attribute_two
… and so on.
The code you give is correct and safe, avoiding “accidentally” binding attributes that should not be bound. If you favor automation over safety and correctness, though, you could use something like…:
However, I would not recommend it (for the reasons implied by the first para;-). OTOH, if you know the names of the attributes you want to copy, the following is just fine:
If you do this kind of thing often, having this code once in a “utilities” module can be a definite win for you!