For example, in file A.py I have functions: a (), b () and c (). I import A.py to B.py, but I want to restrict the functions a () and b (). so that from B.py I will be able to call only c (). How can I do that? Are there public, privates functions?
4 Answers
Really in Python all is public. So if you wish you can call anything.
Standard hiding approach is to name methods with double underscores like __method
. This way Python mangles their names as _class__method
, so they could not be found as __merhod
, but indeed available with long name .

- 12,113
- 5
- 38
- 59
You could make you A.py
a python package with following structure:
B.py
A/
|-- __init__.py
`-- A.py
__init__.py:
from .A import c
A.py ( example ):
def a():
return 'a'
def b():
return 'b'
def c():
print(a(), b(), 'c')
B.py (example):
import A
A.c() # a b c
A.a() # AttributeError: 'module' object has no attribute 'a'
A.b() # not executed because of exception above

- 1,568
- 1
- 11
- 16
-
1But then, you could just do `import A.A`. – eje211 Nov 08 '17 at 05:19
As @Eugene said, nothing is private in Python. This is not something that's missing. Keeping everything public is how Python works.
This is a video of a Java programmer from Nasa who gives a simple example of some advantages of Python over Java: https://youtu.be/4VJoOdpLESw
This is another of a core Python developer, Raymond Hettinger, explaining how to use Python classes: https://youtu.be/HTLu2DFOdTg . It will show why you don't need to keep anything private in Python. Python is not Java; it's not C++. It's not even TypeScript.
Both of these are about Python 2 but apply fairly well to Python 3. There are differences, but the spirit is the same.
If you really want to, you can make members immutable in CPython using the Python API. But that's very advanced. In general, Python is designed with the assumption that people will not willing break encapsulation.

- 2,385
- 3
- 28
- 44
You can try using a _single_leading_underscore.
_single_leading_underscore This convention is used for declaring private variables, functions, methods and classes in a module. Anything with this convention are ignored in from module import *.
However, of course, Python does not supports truly private, so we can not force somethings private ones and also can call it directly from other modules. So sometimes we say it “weak internal use indicator”.

- 59
- 5