Question about sub-packages

Discussion in 'Python' started by Frank Millman, Feb 28, 2012.

  1. Hi all

    This is a follow-up to my recent question about circular imports, but on a
    different subject, hence the new thread.

    My application has grown to the point that it makes sense to split it up
    into sub-packages.

    >From a certain point of view, each package can be said to have an API, not

    just for third-party users of the application, but for other sub-packages
    within the application. In other words, there are a number of functions that
    can be called and a number of objects that can be instantiated from outside
    the sub-package.

    It struck me that, even though I can publish the API, it still requires
    external users to know enough of the internals of the package to know which
    modules to import and which objects to reference. This has two
    disadvantages - it makes it more difficult to understand the API, and it
    makes it more difficult for me to restructure the package internally.

    An alternative is to have a dedicated API within the sub-package, in the
    form of one-line functions that are called externally, and then perform
    whatever action is required internally and return results as appropriate.
    This is easier for users of the sub-package, and allows me to restructure
    the internals of the package without causing problems.

    If this makes sense, my next thought was, where is the best place to put
    this API. Then I thought, why not put it in the __init__.py of the
    sub-package? Then all that the users of the package have to do is import the
    package, and then place calls on it directly.

    I did a quick test and it seems to work. Is this a good idea, or are there
    any downsides?

    Thanks

    Frank Millman
     
    Frank Millman, Feb 28, 2012
    #1
    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. Paul  Smith
    Replies:
    0
    Views:
    748
    Paul Smith
    Nov 18, 2003
  2. Ben
    Replies:
    2
    Views:
    936
  3. David Lyon
    Replies:
    19
    Views:
    606
    David Lyon
    Apr 24, 2009
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,128
    Lawrence D'Oliveiro
    May 20, 2011
  5. Ian
    Replies:
    3
    Views:
    186
Loading...

Share This Page