Exercise: Mutable and Immutable Types

Here’s a list of real-life scenarios below. Your task is to decided whether you need a mutable or immutable type for each one, and write a short sentence or two explaining why you made that choice.

Then, propose a data structure you could use.

In some cases, your answer depends on some assumptions you’ll need to make. The exercise is in thinking about these problems rather than getting the “right” answer.

Here’s an example to clarify the task:

  • Scenario: Individual messages in a chat application
  • Answer: If the chat application allows messages to be edited once they’re posted, then we need a mutable type. Suggestion: dictionary containing the author’s username, timestamp, and the message content. However, if the application doesn’t allow messages to be edited, then we need an immutable type. Suggestion: tuple or namedtuple or data class with frozen=True containing the same fields as above.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.