Junior — Middle
Can you explain what a mutable object is in Python?
sobes.tech AI
Answer from AI
In Python, an object with mutable properties is an object whose state can be changed after creation. For example, lists (list), dictionaries (dict), and sets (set) are mutable. This means that you can add, remove, or modify elements inside the object without creating a new one.
Example:
my_list = [1, 2, 3]
my_list.append(4) # modifying the list by adding an element
print(my_list) # [1, 2, 3, 4]
Unlike immutable objects, such as strings or tuples, mutable objects allow for efficient data manipulation but require caution when passing by reference to avoid unexpected changes.