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

Gabriel Genellina gagsl-py2 at
Sun Aug 19 21:13:15 CEST 2007

On 18 ago, 22:46, beginner <zyzhu2... at> wrote:
> On Aug 18, 8:27 pm, ra... at (Lawrence Oluyede) wrote:
> > beginner <zyzhu2... 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.
> > > 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.
> Thanks a lot. I was using two underscores, __module_method() as my
> static method convention, and then I had some problems calling them
> from inside class methods.- Ocultar texto de la cita -

The convention is to use a single leading underscore _f to indicate
private things.
When you see something like:

from some_module import _function

you know you are messing with internal stuff.

There is another form of import:

from some_module import *

that will import all public names defined in some_module, into the
current namespace. By default, the public names are all names not
beginning with "_" - but you can customize it defining __all__ which
must be the list of public names. (Note that this form of import is
strongly discouraged).
For more info, see the Reference Manual: <

Gabriel Genellina

More information about the Python-list mailing list