324

According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html:

Like most languages, Python has the concept of private elements:

  • Private functions, which can't be called from outside their module

However, if I define two files:

#a.py
__num=1

and:

#b.py
import a
print a.__num

when i run b.py it prints out 1 without giving any exception. Is diveintopython wrong, or did I misunderstand something? And is there some way to do define a module's function as private?

Community
  • 1
  • 1
olamundo
  • 23,991
  • 34
  • 108
  • 149
  • It's not that diveintopython is wrong, but in their example: `>>> import fileinfo >>> m = fileinfo.MP3FileInfo() >>> m.__parse("/music/_singles/kairo.mp3") 1 Traceback (innermost last): File "", line 1, in ? AttributeError: 'MP3FileInfo' instance has no attribute '__parse'` fileinfo.MP3FileInfo() is an instance of class. Which gives this exception when you use double underscore. Whereas in your case, you didn't create a class, you just created a module. See also: https://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private – Homero Esmeraldo Apr 24 '18 at 23:18

11 Answers11

461

In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't force it (any more than you can in real life;-). 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"...!-))

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 107
    Your note on C++ is incorrect. By using #define private public you're changing the code that gets sent to the compiler, which is where the name mangling takes place. – rhinoinrepose Apr 05 '11 at 22:14
  • 17
    Also the C++ mangling is obscure, but hardly secret. You can "introspect" a binary produced by C++ too. OT, sorry. – Prof. Falken Aug 22 '12 at 12:50
  • 62
    As an update to @rhinoinrepose, it is not just incorrect, it is [undefined behavior according to the standard](https://stackoverflow.com/questions/9109377/is-it-legal-to-redefine-a-c-keyword) to redefine a keyword with a preprocessor macro. – Cory Kramer Jun 08 '15 at 20:26
  • 2
    You can use a closure to make a variable private and then return the variables you want to export. – Edgar Klerks Jul 07 '15 at 12:15
  • 3
    @AlexMartelli Isn't `static void foo()` as private as it gets. It is at least hidden to the linker, and the function may be removed entirely by inlining. – user877329 Jun 18 '17 at 13:16
364

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
>>>
NotAnAmbiTurner
  • 2,553
  • 2
  • 21
  • 44
mjv
  • 73,152
  • 14
  • 113
  • 156
  • 1
    Well, TIL. Any reason why they don't enforce module-level `__private_function`, though? I ran into this and got into errors because of it. – Santa Apr 16 '10 at 21:04
  • 1
    Thank you for the kind words @Terrabits, but I gladly stand second to Alex (well behind!) on all things 'Python'. Furthermore his answers are typically more concise and humorous while retaining a high level of authoritativeness given Alex extensive contributions to the language and the community. – mjv May 05 '17 at 14:42
  • 3
    @mjv This was such a helpful explanation! Thank you! I have been quite puzzled about this behavior for a while. I do wish the choice had been to throw some kind of error other than an AttributeError if you tried to directly access the class private; perhaps a "PrivateAccessError" or something would have been more explicit/helpful. (Since getting the error that it doesn't have an attribute isn't *really* true). – HFBrowning Mar 16 '18 at 17:43
  • 2
    [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles) `_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose names start with an underscore.` – Darren Ng Jul 31 '21 at 10:38
102

This question was not fully answered, since module privacy is not purely conventional, and since using import may or may not recognize module privacy, depending on how it is used.

If you define private names in a module, those names will be imported into any script that uses the syntax, 'import module_name'. Thus, assuming you had correctly defined in your example the module private, _num, in a.py, like so..

#a.py
_num=1

..you would be able to access it in b.py with the module name symbol:

#b.py
import a
...
foo = a._num # 1

To import only non-privates from a.py, you must use the from syntax:

#b.py
from a import *
...
foo = _num # throws NameError: name '_num' is not defined

For the sake of clarity, however, it is better to be explicit when importing names from modules, rather than importing them all with a '*':

#b.py
from a import name1 
from a import name2
...
Ben Wilhelm
  • 1,898
  • 3
  • 15
  • 12
  • 1
    where do you specify which functions/libraries are imported? in the __init__.py? – FistOfFury Aug 24 '16 at 21:40
  • There is no risk of name collisions when `_names` are invoked with `import a` -- they are accesses as `a._names` when using this style. – Josiah Yoder Sep 07 '17 at 15:52
  • 1
    @FistOfFury Yes, you specify the functions imported in the `__init__.py` file. See [here](https://stackoverflow.com/a/64130/534238) for some help on that. – Mike Williamson Jan 19 '18 at 00:36
39

Python allows for private class members with the double underscore prefix. This technique doesn't work at a module level so I am thinking this is a mistake in Dive Into Python.

Here is an example of private class functions:

class foo():
    def bar(self): pass
    def __bar(self): pass

f = foo()
f.bar()   # this call succeeds
f.__bar() # this call fails
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 5
    I think the OP's intent is to write functions that are not accessible outside of, for example, a commercial package. In that regard, this answer isn't complete. The __bar() function is still accessible from outside through f._foo__bar(). Therefore, the double-leading underscores do not make it private. – Sevak Avakians May 29 '15 at 12:11
  • This is useful, I was looking for a function accessible from within a class but not outside the class. – SharksRule Jun 06 '22 at 11:13
  • It is important to notice that `f._foo__bar()` will not fail and expose the function. – Avizipi Jul 27 '23 at 08:38
33

You can add an inner function:

def public(self, args):
   def private(self.root, data):
       if (self.root != None):
          pass #do something with data

Something like that if you really need that level of privacy.

Ilian Zapryanov
  • 1,132
  • 2
  • 16
  • 28
  • 10
    Why is this not the best answer? – safay Nov 09 '18 at 21:28
  • 5
    I guess its because the nested function is not reusable anywhere else in the module so there is no benefit to defining a function, unless it is to be used multiple times inside the outer function. In this case I feel it would be more readable to just inline the code. – Pradyumna Das Aug 25 '20 at 09:22
6

This is an ancient question, but both module private (one underscore) and class-private (two underscores) mangled variables are now covered in the standard documentation:

The Python Tutorial » Classes » Private Variables

user1338062
  • 11,939
  • 3
  • 73
  • 67
2

Sorry if I'm late to answer, but in a module, you can define the packages to "export" like this:

mymodule
  __init__.py
  library.py
main.py

mymodule/library.py

# 'private' function
def _hello(name):
    return f"Hello {name}!"

# 'public' function which is supposed to be used instead of _hello
def hello():
    name = input('name: ')
    print(_hello(name))

mymodule/__init__.py

# only imports certain functions from library
from .library import hello

main.py

import mymodule
mymodule.hello()

Nevertheless, functions can still be accessed,

from mymodule.library import _hello
print(_hello('world'))

But this approach makes it less obvious

ajskateboarder
  • 379
  • 4
  • 11
1

embedded with closures or functions is one way. This is common in JS although not required for non-browser platforms or browser workers.

In Python it seems a bit strange, but if something really needs to be hidden than that might be the way. More to the point using the python API and keeping things that require to be hidden in the C (or other language) is probably the best way. Failing that I would go for putting the code inside a function, calling that and having it return the items you want to export.

0

For methods: (I am not sure if this exactly what you want)

print_thrice.py

def private(method):
    def methodist(string):
        if __name__ == "__main__":
            method(string)
    return methodist
    
@private
def private_print3(string):
    print(string * 3)

private_print3("Hello ") # output: Hello Hello Hello

other_file.py

from print_thrice import private_print3
private_print3("Hello From Another File? ") # no output

This is probably not a perfect solution, as you can still "see" and/or "call" the method. Regardless, it doesn't execute.

0

See PEP8 guideline:

Method Names and Instance Variables

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

Use one leading underscore only for non-public methods and instance variables.

To avoid name clashes with subclasses, use two leading underscores to invoke Python’s name mangling rules.

Python mangles these names with the class name: if class Foo has an attribute named __a, it cannot be accessed by Foo.__a. (An insistent user could still gain access by calling Foo._Foo__a.) Generally, double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed.

Designing for Inheritance

Always decide whether a class’s methods and instance variables (collectively: “attributes”) should be public or non-public. If in doubt, choose non-public; it’s easier to make it public later than to make a public attribute non-public.

Public attributes are those that you expect unrelated clients of your class to use, with your commitment to avoid backwards incompatible changes. Non-public attributes are those that are not intended to be used by third parties; you make no guarantees that non-public attributes won’t change or even be removed.

We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work).

aduverger
  • 151
  • 1
  • 6
-24

Python has three modes via., private, public and protected .While importing a module only public mode is accessible .So private and protected modules cannot be called from outside of the module i.e., when it is imported .

sravan
  • 1