python classes/file structure

Discussion in 'Python' started by codecraig, Apr 21, 2005.

  1. codecraig

    codecraig Guest

    What is the best/common way to structure ur python code for an
    application?

    For example...if I create some custom GUI widgets I have this

    C:\stuff
    --> gui
    --: MyCustomWidget.py
    --: TestWidgets.py

    so MyCustomWidget.py has one class, class MyCustomWidget: ...

    so from TestWidgets.py i have to do this

    from gui import *

    widget = gui.MyCustomWidget.MyCustomWidge()

    ....seems weird, how should I structure this? Is it not common to have
    one class in a .py?

    thanks
    codecraig, Apr 21, 2005
    #1
    1. Advertising

  2. codecraig

    Steve Holden Guest

    codecraig wrote:
    > What is the best/common way to structure ur python code for an
    > application?
    >
    > For example...if I create some custom GUI widgets I have this
    >
    > C:\stuff
    > --> gui
    > --: MyCustomWidget.py
    > --: TestWidgets.py
    >
    > so MyCustomWidget.py has one class, class MyCustomWidget: ...
    >
    > so from TestWidgets.py i have to do this
    >
    > from gui import *
    >
    > widget = gui.MyCustomWidget.MyCustomWidge()
    >
    > ...seems weird, how should I structure this? Is it not common to have
    > one class in a .py?
    >
    > thanks
    >

    gui/__init__.py should import MyCustomWidget. Then

    from gui import *

    will inject the "MyCustomWidget" name into the namespace of the
    TestWidgets module along with all other names defined by gui/__init__.py
    - you can control this by putting the names as strings in the "__all__"
    variable in __init__.py.

    regards
    Steve
    --
    Steve Holden +1 703 861 4237 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Steve Holden, Apr 21, 2005
    #2
    1. Advertising

  3. codecraig

    codecraig Guest

    Thanks, but I am not familiar with the "__all__" variable, could u give
    me an example?

    Without using, __all__....would i do this in my __init__.py?

    import MyCustomWidget1
    import MyCustomWidget2
    import MyCustomWidget3

    etc?
    codecraig, Apr 21, 2005
    #3
  4. codecraig

    codecraig Guest

    also is it common to have one class per file?

    seems weird to have, MyCustomWidget.MyCustomWidget

    thanks
    codecraig, Apr 21, 2005
    #4
  5. codecraig

    Steve Holden Guest

    codecraig wrote:
    > Thanks, but I am not familiar with the "__all__" variable, could u give
    > me an example?
    >
    > Without using, __all__....would i do this in my __init__.py?
    >
    > import MyCustomWidget1
    > import MyCustomWidget2
    > import MyCustomWidget3
    >
    > etc?
    >

    Yes, correct. __all__ just limits the names that are imported into the
    receiving namespace with "from module import *".

    regards
    Steve
    --
    Steve Holden +1 703 861 4237 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Steve Holden, Apr 21, 2005
    #5
  6. codecraig

    Steve Holden Guest

    codecraig wrote:
    > also is it common to have one class per file?
    >
    > seems weird to have, MyCustomWidget.MyCustomWidget
    >
    > thanks
    >

    Well, this *is* fairly normal, but you can manage the namespace to your
    advantage if you wish. So, for example, gui/__init__.py could do

    from CustomWidget import CustomWidget

    and then

    from gui import *

    sees CustomWidget.CustomWidget simply as CustomWidget.

    regards
    Steve
    --
    Steve Holden +1 703 861 4237 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Steve Holden, Apr 21, 2005
    #6
  7. On Thursday 21 April 2005 08:48 am, codecraig wrote:
    > widget = gui.MyCustomWidget.MyCustomWidge()
    >
    > ...seems weird, how should I structure this? Is it not common to have
    > one class in a .py?


    No, it isn't really. Usually, there will be several related classes in a single
    module. Only if the classes get really large would I bother to separate
    them out into separate files. "One class per module" is perlthink. ;-)

    Someone has already mentioned using __init__.py to control how your
    classes are assembled into the package namespace.

    You should also be aware that:

    from gui.MyCustomWidget import MyCustomWidget

    is a pretty common idiom too.

    Cheers,
    Terry


    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
    Terry Hancock, Apr 23, 2005
    #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. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    734
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,186
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    588
  4. Replies:
    9
    Views:
    25,227
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    762
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page