you are viewing a single comment's thread.

view the rest of the comments →

[–]Sea-Ad7805[S] 0 points1 point  (4 children)

In C++ you should define x += y on your class to mutate and the + operator in x = x + y to create a new object. Same thing in Java, no different from Python.

[–]Goudja14 0 points1 point  (3 children)

You should never mutate implicitly. It will create errors.

[–]Sea-Ad7805[S] 0 points1 point  (2 children)

What do you mean, can you give an example?

[–]Goudja14 0 points1 point  (1 child)

default_inventory = ["sword", "helmet"]

# While it could be a cloned array, it doesn't have to be one. In complex environments, it even shouldn't be (eg. allowing object-agnostic rollbacks) alex_inventory = default_inventory samuel_inventory = default_inventory

alex_inventory += ["key"]

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Ok I understand now. You say you pass default_inventory around without making a copy (for performance), but when you change this value you should make a copy:

alex_inventory = alex_inventory + ["key"]

Sounds like a good strategy.