Private/public module members

Elbert Lev elbertlev at hotmail.com
Thu Jun 24 15:37:44 CEST 2004


Hi, all!

In accordance with Python documentation, there are 2 ways to hide
data/methods inside the module (make them private):

1. have "public" members defined in __all__ list
2. start "private" members names with underscore.

Both methods for some reason "behave strange".

Here are 2 modules and the output:

#file: main.py ########################
import foo
print dir(foo)
foo.b()
foo._c()
foo.a()

#file: foo.py######################
import sys
__all__ = ["a"]
_private = 56
def b(): 	print 'b'
def _c(): 	print '_c'
def a(): 	print 'a'

Run main.py and here is the output:

['__all__', '__builtins__', '__doc__', '__file__', '__name__', '_c',
'_private', 'a', 'b', 'sys']
b
_c
a

Not only doc(foo) has '_c', '_private' and 'b', but one can call them
from outside the module.
It this "by design"?



More information about the Python-list mailing list