Returning an Independent Collection from an Object

An object can protect writable internal state by returning a separate collection or an immutable view with a specified lifetime.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace an accidental edit
  2. Check depth

Trace an accidental edit

A caller receives the collection of scheduled jobs and removes the first to create a preview. If it was the object's own mutable list, that edit changes the actual schedule. Encapsulation helps define which methods may perform that edit.

Check depth

A separate outer list still shares its job records. If the caller may edit them independently, return a snapshot of their relevant fields too. See immutable record snapshots.[1]

References

  1. Python Software Foundation, Python Tutorial: Classes ↩