Skip to content

Conversation

@colombod
Copy link

Summary

Adds a new principle to PYTHON_BEST_PRACTICES.md addressing the critical anti-pattern of accessing private attributes via getattr(obj, '_internal', ...).

Motivation

This PR was motivated by microsoft/amplifier-app-cli#70 which demonstrates a fix that reaches into private internal state instead of using public APIs:

# The anti-pattern from PR #70
bundle_resolver = getattr(resolver, "_bundle", resolver)  # Unwrap layer 1
activator = getattr(bundle_resolver, "_activator", None)   # Unwrap layer 2

The comment "Pattern from session_spawner.py" shows this anti-pattern is spreading across the codebase.

What's Added

New Principle 7: Respect Encapsulation Boundaries covering:

  • Why accessing private state is dangerous (changes without warning, multiplied fragility)
  • Table of anti-patterns and correct approaches
  • Real code example from PR #70
  • Decision framework: when tempted to access _private, what to do instead
  • Litmus test for detecting the problem

Related


🤖 Generated with Amplifier

Co-Authored-By: Amplifier 240397093+microsoft-amplifier@users.noreply.github.com

Add principle to PYTHON_BEST_PRACTICES.md about not accessing private
attributes from outside a class. Includes table of anti-patterns and
code example.

Motivated by microsoft/amplifier-app-cli#70.

🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier)

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
@colombod
Copy link
Author

Closing - the foundation PR (microsoft/amplifier-foundation#42) covers the general principle with language-agnostic examples, which is sufficient.

@colombod colombod closed this Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant