Packaging question

Discussion in 'Python' started by snorble, Jul 1, 2010.

  1. snorble

    snorble Guest

    My question is, why do the modules bar and foo show up in mypack's
    dir()? I intend for Foo (the class foo.Foo) and Bar (the class
    bar.Bar) to be there, but was not sure about the modules foo and bar.

    My big picture intention is to create smaller modules, but more of
    them (like I am used to doing with C++), and then use a package to
    organize the namespace so I'm not typing out excessively long names
    and making the code less readable. Is that a reasonable approach to
    developing Python programs?

    $ ls mypack/*.py
    bar.py
    foo.py
    __init__.py

    $ cat mypack/__init__.py
    from foo import Foo
    from bar import Bar

    $ python
    Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mypack
    >>> dir(mypack)

    ['Bar', 'Foo', '__builtins__', '__doc__', '__file__', '__name__',
    '__package__', '__path__', 'bar', 'foo']
    >>>
    snorble, Jul 1, 2010
    #1
    1. Advertising

  2. snorble

    Peter Otten Guest

    snorble wrote:

    > My question is, why do the modules bar and foo show up in mypack's
    > dir()? I intend for Foo (the class foo.Foo) and Bar (the class
    > bar.Bar) to be there, but was not sure about the modules foo and bar.


    > $ ls mypack/*.py
    > bar.py
    > foo.py
    > __init__.py
    >
    > $ cat mypack/__init__.py
    > from foo import Foo
    > from bar import Bar
    >
    > $ python
    > Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
    > (Intel)] on win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> import mypack
    >>>> dir(mypack)

    > ['Bar', 'Foo', '__builtins__', '__doc__', '__file__', '__name__',
    > '__package__', '__path__', 'bar', 'foo']



    How is Python to know that you won't perform an

    import mypack.foo

    afterwards? After this statement foo must be an attribute of mypack. But
    when mypack.foo has been imported before this just performs

    mypack = sys.modules["mypack"]

    If the foo attribute weren't added by

    from foo import Foo

    the caching mechanism would not work. While

    import mypack.foo

    might also have been implemented as

    mypack = sys.modules["mypack"]
    if not hasattr(mypack", "foo"):
    mypack.foo = sys.modules["mypack.foo"]

    I think that would have been a solution to a non-problem. If you're sure you
    don't need to access mypack.foo directly you can add

    del foo

    to mypack/__init__.py, but don't complain when you get bitten by

    >>> import mypack.foo
    >>> mypack.foo

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'foo'

    > My big picture intention is to create smaller modules, but more of
    > them (like I am used to doing with C++), and then use a package to
    > organize the namespace so I'm not typing out excessively long names
    > and making the code less readable. Is that a reasonable approach to
    > developing Python programs?


    I like to put related classes and functions into a single file. As a rule of
    thumb, when a class needs a file of its own the class is too big...

    Peter
    Peter Otten, Jul 2, 2010
    #2
    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. Replies:
    1
    Views:
    273
  2. Alan Isaac
    Replies:
    0
    Views:
    310
    Alan Isaac
    Jun 23, 2007
  3. Scott Sharkey

    naive packaging question

    Scott Sharkey, Mar 26, 2008, in forum: Python
    Replies:
    1
    Views:
    249
    Carl Banks
    Mar 26, 2008
  4. Replies:
    1
    Views:
    213
    Chris Rebert
    Feb 10, 2009
  5. Tadatoshi Takahashi

    Question: Ruby application packaging

    Tadatoshi Takahashi, Oct 11, 2006, in forum: Ruby
    Replies:
    0
    Views:
    99
    Tadatoshi Takahashi
    Oct 11, 2006
Loading...

Share This Page