liveBook · Manning

Private member

As explained in Grokking Data Structures

A private attribute is an object attribute intended to be accessed only within the implementation that defines it.

A private member is an attribute or method marked for internal use by its defining implementation. In Python examples, a leading underscore signals that clients should use the class's public interface instead. section 6.8 section 8.12

Keep members private when exposing them would:

  • Return implementation objects such as linked-list nodes instead of useful client data. section 6.8
  • Expose internal structure and make future implementation changes break client code. section 6.8
  • Allow callers to access mutable data or bypass necessary edge-case checks. section 8.12

Internal search in a linked list

The _search method returns a node for use by the implementation, while a public method can expose only the result clients need. section 6.8

Private tree search

A tree can keep _search private and offer contains publicly, preventing clients from depending on tree nodes while preserving internal search logic. section 11.8

How Grokking Data Structures covers it

How deep it goes
Explains private methods and attributes through linked-list, stack, and tree access problems, stopping short of formal access enforcement.
Examples
Uses _search in linked lists and trees, plus direct _head access during stack peek.
What it assumes
Assumes basic classes and data structures while teaching why clients should avoid direct access to internal nodes and attributes.
What you can do after
Apply it via leading-underscore naming and public methods that hide nodes, heads, and other structural details.
How it connects
Connects private members with encapsulation, public interfaces, linked lists, stacks, binary search trees, and mutable objects.

Also explained in

Compare how 12 books explain private member →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.