Why Some Methods Change The Object Permanently While Others Don't?
Solution 1:
Does it depend on simply how the method is built?
I guess you could say "yes", but the in-place methods - ones that have a "permanent" change - are usually found on mutable
objects such as lists.
Mutable objects can change their value over their lifetime which means there isn't any point in returning a new object when these methods are called.
On the other hand, the methods that return a new object are for immutable objects. Immutable means the object has a fixed value and cannot change. Therefore, the in-place methods cannot work as they would need to alter the value of the object which isn't possible for immutable objects. This is why the methods return a new object, which is usually assigned back to the name that it was originally bound to, creating the effect of mutability, one could say.
Post a Comment for "Why Some Methods Change The Object Permanently While Others Don't?"