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

Discussion in 'Python' started by beginner, Aug 19, 2007.

  1. beginner

    beginner Guest

    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
     
    beginner, Aug 19, 2007
    #1
    1. Advertising

  2. beginner <> 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
     
    Lawrence Oluyede, Aug 19, 2007
    #2
    1. Advertising

  3. beginner

    beginner Guest

    On Aug 18, 8:27 pm, (Lawrence Oluyede) wrote:
    > beginner <> 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.
     
    beginner, Aug 19, 2007
    #3
  4. 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
     
    Bjoern Schliessmann, Aug 19, 2007
    #4
  5. beginner

    beginner Guest

    On Aug 19, 7:45 am, Bjoern Schliessmann <usenet-
    > 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.
     
    beginner, Aug 19, 2007
    #5
  6. On 18 ago, 22:46, beginner <> wrote:
    > On Aug 18, 8:27 pm, (Lawrence Oluyede) wrote:
    > > beginner <> 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
     
    Gabriel Genellina, Aug 19, 2007
    #6
  7. 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.
     
    Bjoern Schliessmann, Aug 19, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spamfurnace
    Replies:
    1
    Views:
    2,671
    Scott M.
    May 24, 2004
  2. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    6,053
    Patrick Olurotimi Ige
    Jun 15, 2005
  3. Martin
    Replies:
    0
    Views:
    474
    Martin
    Apr 24, 2008
  4. Mark Space
    Replies:
    1
    Views:
    382
    Mark Space
    Apr 26, 2008
  5. Jurjen de Groot
    Replies:
    0
    Views:
    335
    Jurjen de Groot
    May 19, 2008
Loading...

Share This Page