Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to make a module function visible only inside the module?

Reply
Thread Tools

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

 
 
beginner
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
 
 
 
Lawrence Oluyede
Guest
Posts: n/a
 
      08-19-2007
beginner <(E-Mail Removed)> 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():
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
 
Reply With Quote
 
 
 
 
beginner
Guest
Posts: n/a
 
      08-19-2007
On Aug 18, 8:27 pm, (E-Mail Removed) (Lawrence Oluyede) wrote:
> beginner <(E-Mail Removed)> 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():
> 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.

 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
beginner
Guest
Posts: n/a
 
      08-19-2007
On Aug 19, 7:45 am, Bjoern Schliessmann <usenet-
(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-19-2007
On 18 ago, 22:46, beginner <(E-Mail Removed)> wrote:
> On Aug 18, 8:27 pm, (E-Mail Removed) (Lawrence Oluyede) wrote:
> > beginner <(E-Mail Removed)> 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: <http://docs.python.org/ref/
import.html>

--
Gabriel Genellina

 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      08-19-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to make a Java class only visible inside the package & notvisible to other packages? Mark Space Java 1 04-26-2008 01:56 AM
Re: How to make a Java class only visible inside the package & notvisible to other packages? Martin Java 0 04-24-2008 04:15 PM
How to make a hyperlink Visible or not visible in DataList Patrick Olurotimi Ige ASP .Net 7 06-15-2005 12:01 PM
Panel.Visible = True also make child controls visible. spamfurnace ASP .Net 1 05-24-2004 03:07 AM



Advertisments