By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Aug 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
beginner <zy*******@gmail.comwrote:
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
Aug 19 '07 #2

P: n/a
On Aug 18, 8:27 pm, ra...@dot.com (Lawrence Oluyede) wrote:
beginner <zyzhu2...@gmail.comwrote:
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.

Aug 19 '07 #3

P: n/a
beginner wrote:
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

--
BOFH excuse #183:

filesystem not big enough for Jumbo Kernel Patch

Aug 19 '07 #4

P: n/a
On Aug 19, 7:45 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
beginner wrote:
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

--
BOFH excuse #183:

filesystem not big enough for Jumbo Kernel Patch
I just started learning the language. I wasn't aware of the PEP.

Aug 19 '07 #5

P: n/a
On 18 ago, 22:46, beginner <zyzhu2...@gmail.comwrote:
On Aug 18, 8:27 pm, ra...@dot.com (Lawrence Oluyede) wrote:
beginner <zyzhu2...@gmail.comwrote:
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: <http://docs.python.org/ref/
import.html>

--
Gabriel Genellina

Aug 19 '07 #6

P: n/a
beginner wrote:
I just started learning the language. I wasn't aware of the PEP.
Mh, two postings before Lawrence already mentioned it.

I suggest looking through the BeginnersGuide.

http://wiki.python.org/moin/BeginnersGuide

Regards,
Björn

--
BOFH excuse #203:

Write-only-memory subsystem too slow for this machine. Contact your
local dealer.

Aug 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.