How to make a module function visible only inside the module?

Lawrence Oluyede raims at dot.com
Sat Aug 18 21:27:42 EDT 2007


beginner <zyzhu2000 at gmail.com> wrote:
> Is there any equivalent version of C's static function in Python. I
> know I can make a class function private by starting a function name
> with two underscores, but it does not work with module functions.

The trick for the name mangling does not work at module level. Anyway,
if you read the PEP 8 [1] you can correctly write your code following a
well known coding standard. A function like this:

def _f():
    pass

is meant to be private, you can also state it in the function's
docstring to be more clear, if you want, but it's not necessary

> For exmaple, __func1 is still visible outside the module.

Yes, and _f() will also be. There's no such thing as enforcing
encapsulation in Python, even the "__method()" trick can be easily
bypassed if you have to.

1 - <http://www.python.org/dev/peps/pep-0008/>

HTH

-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list