472,783 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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

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
6 8829
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bobbak | last post by:
Hello All, I was wondering if it is possible to do this; I have a form that has number of text boxes that when a button is clicked it turns into combo boxes, simply by toggling the visibility to...
11
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
1
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
13
by: Kirk | last post by:
I have been reading Scott Allen's article on Master Pages (http:// odetocode.com/Articles/450.aspx) but I am having problems understanding a concept. Specifically, I have created a property...
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
2
by: jesus4gaveme03 | last post by:
I ceated a automatic split form from a table called "Master NSN List." I then added 2 buttons "cmdShowHide" which toggles between showing and hiding the form section giving more room for the...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.