Fundamentals 3 min read

Why Python’s Double‑Underscore Variables Aren’t Truly Private (And What Really Happens)

This article explains Python’s name‑mangling for double‑underscore attributes, how they can still be accessed indirectly, the effects of instance attribute shadowing on class variables, and why deleting certain dict attributes raises errors due to CPython’s underlying implementation.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Python’s Double‑Underscore Variables Aren’t Truly Private (And What Really Happens)

In Python classes, attributes and methods are normally accessible directly through an instance.

To hide an internal attribute from external access, prefix its name with two underscores (__), which triggers name‑mangling and makes it a private variable that can only be accessed inside the class.

Although external code cannot directly access a private attribute, it can still be accessed or modified indirectly via class methods, such as getter and setter functions.

Private attributes prefixed with double underscores are not absolutely inaccessible; they can be accessed using the mangled name _ClassName__attribute.

Python also exhibits an interesting phenomenon where external variables can shadow class variables. Assigning a value to an instance attribute creates the attribute on the instance if it does not already exist, potentially masking a class attribute of the same name and causing side effects.

The following images illustrate code examples of private attributes, getter/setter methods, and attribute shadowing:

private attribute example
private attribute example
getter and setter methods
getter and setter methods
attribute shadowing example
attribute shadowing example

Another example demonstrates why deleting a dict attribute can raise an error: CPython is implemented in C, where strings are immutable character arrays, but dictionaries are stored in non‑contiguous memory that can be modified in place, leading to different behavior when attempting to delete attributes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonprivate attributesclass methodsname manglingattribute shadowinginstance vs class variables
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.