python classes/file structure

C

codecraig

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
 
S

Steve Holden

codecraig said:
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
 
C

codecraig

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?
 
C

codecraig

also is it common to have one class per file?

seems weird to have, MyCustomWidget.MyCustomWidget

thanks
 
S

Steve Holden

codecraig said:
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
 
S

Steve Holden

codecraig said:
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
 
T

Terry Hancock

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top