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

B

beginner

Hi Everyone,

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.

mymodule.py
"""my module""

def __func1():
print "Hello"



main.py
import mymodule

mymodule.__func1()

Thanks,
Geoffrey
 
L

Lawrence Oluyede

beginner said:
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
 
B

beginner

beginner said:
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

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

Bjoern Schliessmann

beginner said:
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.

*Please* do yourself and other people that sometime may have to read
your code a favor and write code at least loosely oriented to
PEP 8.

BTW, Python has no "static methods" at module level. And I suppose
what you call "class methods" actually aren't.

Regards,


Björn
 
B

beginner

*Please* do yourself and other people that sometime may have to read
your code a favor and write code at least loosely oriented to
PEP 8.

BTW, Python has no "static methods" at module level. And I suppose
what you call "class methods" actually aren't.

Regards,

Björn

I just started learning the language. I wasn't aware of the PEP.
 
G

Gabriel Genellina

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: <http://docs.python.org/ref/
import.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top