In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't force it. A single leading underscore means you're not supposed to access it "from the outside" -- two leading underscores (w/o trailing underscores) carry the message even more forcefully... but, in the end, it still depends on social convention and consensus: Python's introspection is forceful enough that you can't handcuff every other programmer in the world to respect your wishes.

((Btw, though it's a closely held secret, much the same holds for C++: with most compilers, a simple #define private public line before #includeing your .h file is all it takes for wily coders to make hash of your "privacy"...!-))

Answer from Alex Martelli on Stack Overflow
Top answer
1 of 11
500

In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't force it. A single leading underscore means you're not supposed to access it "from the outside" -- two leading underscores (w/o trailing underscores) carry the message even more forcefully... but, in the end, it still depends on social convention and consensus: Python's introspection is forceful enough that you can't handcuff every other programmer in the world to respect your wishes.

((Btw, though it's a closely held secret, much the same holds for C++: with most compilers, a simple #define private public line before #includeing your .h file is all it takes for wily coders to make hash of your "privacy"...!-))

2 of 11
388

There may be confusion between class privates and module privates.

A module private starts with one underscore
Such a element is not copied along when using the from <module_name> import * form of the import command; it is however imported if using the import <module_name> syntax (see Ben Wilhelm's answer)

Simply remove one underscore from the a.__num of the question's example and it won't show in modules that import a.py using the from a import * syntax.

A class private starts with two underscores (aka dunder i.e. d-ouble under-score)

Such a variable has its name "mangled" to include the classname etc.

It can still be accessed outside of the class logic, through the mangled name.

Although the name mangling can serve as a mild prevention device against unauthorized access, its main purpose is to prevent possible name collisions with class members of the ancestor classes. See Alex Martelli's funny but accurate reference to consenting adults as he describes the convention used in regards to these variables.

>>> class Foo(object):
...    __bar = 99
...    def PrintBar(self):
...        print(self.__bar)
...
>>> myFoo = Foo()
>>> myFoo.__bar  #direct attempt no go
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__bar'
>>> myFoo.PrintBar()  # the class itself of course can access it
99
>>> dir(Foo)    # yet can see it
['PrintBar', '_Foo__bar', '__class__', '__delattr__', '__dict__', '__doc__', '__
format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__
', '__subclasshook__', '__weakref__']
>>> myFoo._Foo__bar  #and get to it by its mangled name !  (but I shouldn't!!!)
99
>>>
🌐
GeeksforGeeks
geeksforgeeks.org › python › private-methods-in-python
Private Methods in Python - GeeksforGeeks
July 12, 2025 - It is used to hide the inner functionality of any class from the outside world. Private methods are those methods that should neither be accessed outside the class nor by any base class.
Discussions

"private methods" vs method out of class in a python module - Software Engineering Stack Exchange
I have a module with the class Foo. Foo has three methods: The constructor(__init__()) An error handling method (_error_handler()) The method that actually does something. (run()) Then, I have a bu... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
December 21, 2018
Do you use private methods/attributes
I can see why he doesn't care about them and it's not something I'm particularly quick to call out in code review. That said, I think they still have their uses. It's helpful to have an easy way to mark functions as some internal logic to your module/class or as a public function. I think it's good for someone using my code to know it's been designed for them to use foo and they shouldn't need to directly call _bar. They can then do whatever they want with that knowledge, I'm not their mother. More on reddit.com
🌐 r/learnpython
26
20
July 12, 2024
clean code - What is the rule for making functions private in Python modules? - Software Engineering Stack Exchange
I was wondering what's the principle behind it, and whether I should treat all functions from module that are not used anywhere else at the time as private, and mark them with underscore prefix so ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
encapsulation - Why are Python's 'private' methods not actually private? - Stack Overflow
Module attributes named as such will not be copied into an importing module when using the from* method, e.g.: ... However, this is a convention and not a language constraint. These are not private attributes; they can be referenced and manipulated by any importer. Some argue that because of this, Python ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Thibaultbl
thibaultbl.github.io › 2018-07-08-private-class-in-python
Python Private method and functions
Just add two underscore before the method or function name and it become private (also works for attributes), do not add two uderscores at the end of the name, it's for builtin/special method.
🌐
Linux Manual Page
linux.die.net › diveintopython › html › object_oriented_framework › private_functions.html
5.9. Private Functions - Python
Like most languages, Python has the concept of private elements: Private functions, which can't be called from outside their module · Private class methods, which can't be called from outside their class · Private attributes, which can't be accessed from outside their class.
🌐
Medium
medium.com › weekly-python › private-methods-and-functions-in-python-with-access-modifiers-ed59d82fbfd8
Private Methods And Functions In Python With Access Modifiers | by Christopher Franklin | Weekly Python | Medium
January 7, 2021 - Sometimes we want to put restrictions on our class members where they can’t be accessed outside the class or even from any subclasses. In those cases, we can declare a variable or method as private using the __ double underscore.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python private functions
Python Private Functions
March 30, 2025 - Summary: in this tutorial, you’ll learn how to define Python private functions in a module using the __all__ variable.
🌐
DataCamp
datacamp.com › tutorial › python-private-methods-explained
Python Private Methods Explained | DataCamp
March 27, 2024 - Using a private method here helps encapsulate the logic for updating the balance, ensuring that it can only be done in controlled ways defined by the class. Feel free to create a new Python file, copy and paste the code above and below, and execute it to see it yourself.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › do you use private methods/attributes
r/learnpython on Reddit: Do you use private methods/attributes
July 12, 2024 -

Hi,

Some time ago I work with a senior that don't use private method because he said you could access them anyway, so it didn't make sense, in other languages it did, because it was impossible to access it and that's why it was useful, but not in Python.

What do you think, have sense?

(I want to create a survey but I can't, so I'll put two comments, and wait for your upvotes)

🌐
Medium
purnimachowrasia.medium.com › what-is-private-method-in-python-778bd6e77bad
What is Private method in Python? | by Purnima Chowrasia | Medium
March 26, 2025 - The main purpose of private method is for internal use in specific class or module. An example is, a method inside a class relying on a private method to check wether a user is logged in or not.
🌐
Delft Stack
delftstack.com › home › howto › python › python private method
Private Methods in Python | Delft Stack
October 10, 2023 - To declare a private method in Python, insert double underscores into the beginning of the method name.
🌐
DocStore
docstore.mik.ua › orelly › other › python › 0596001886_pythonian-chp-7-sect-1.html
Module-private variables
A module is a Python object with arbitrarily named attributes that you can bind and reference. The Python code for a module named aname normally resides in a file named aname.py, as covered in Section 7.2 later in this chapter · In Python, modules are objects (values) and are handled like ...
🌐
Andrea Minini
andreaminini.net › computer-science › python › private-methods-and-attributes-in-a-python-class
Private Methods and Attributes in a Python Class - Andrea Minini
Since this is a private method, it can’t be accessed from the rest of the program. If you try to call it from an instance, the Python interpreter won’t find an attribute with that name and will return an error message. >>> obj = Example() >>> obj.__privatemethod() Traceback (most recent call last): File "<pyshell#5>", line 1, in <module...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python private method
Python Private Method | Rules and regulations of Python private method
April 4, 2023 - However, as per the convention, a method or name prefixed with one underscore is treated as non-public. Hence, private members can be accessed outside the class by making it nonpublic like _classname__privatevariable. This is called “name mangling”. In a nutshell, we can say Python not truly supports the fundamentals of private.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai