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

Lawrence Oluyede raims at
Sun Aug 19 03:27:42 CEST 2007

beginner <zyzhu2000 at> 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():

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 - <>


Lawrence, -
"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