hiding modules in __init__.py

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brendan Miller

    #1

    hiding modules in __init__.py

    How would I implement something equivalent to java's package private in
    python?

    Say if I have

    package/__init__.py
    package/utility_module. py

    and utility_module. py is an implementation detail subject to change.

    Is there some way to use __init__.py to hide modules that I don't want
    clients to see? Or is the best practice just to name the module you don't
    want clients to use _utility_module and have it private by convention?

    Thanks,
    Brendan
  • Chris Rebert

    #2
    Re: hiding modules in __init__.py

    On Sat, Oct 18, 2008 at 12:03 PM, Brendan Miller <catphive@catph ive.netwrote:
    How would I implement something equivalent to java's package private in
    python?
    >
    Say if I have
    >
    package/__init__.py
    package/utility_module. py
    >
    and utility_module. py is an implementation detail subject to change.
    >
    Is there some way to use __init__.py to hide modules that I don't want
    clients to see? Or is the best practice just to name the module you don't
    want clients to use _utility_module and have it private by convention?
    Generally the latter on account of Python's "we are all consenting
    adults here" philosophy.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    Comment

    • Gabriel Genellina

      #3
      Re: hiding modules in __init__.py

      En Sat, 18 Oct 2008 16:03:19 -0300, Brendan Miller <catphive@catph ive.net>
      escribió:
      How would I implement something equivalent to java's package private in
      python?
      >
      Say if I have
      >
      package/__init__.py
      package/utility_module. py
      >
      and utility_module. py is an implementation detail subject to change.
      >
      Is there some way to use __init__.py to hide modules that I don't want
      clients to see? Or is the best practice just to name the module you don't
      want clients to use _utility_module and have it private by convention?
      If you don't import utility_module in __init__ (or delete the name after
      using it), it won't show up if someone does "from package import *", nor
      in dir(package). Plus if you don't menction it in the docs, the only way
      to discover it would be to look at the directory contents - to "peek the
      implementation" , I'd say.
      You could always name it _utility_module .py if you want to make perfectly
      clear that it's for internal use only, but I've seldom seen such module
      names.

      --
      Gabriel Genellina

      Comment

      Working...