Since I prefer small files, I usually put one "public" class in a Python module. I call the module with the same name as the class that it contains. So, for example, the class ToolSetwill be defined in ToolSet.py.
Inside the package, if another module needs to create an object of the ToolSet class, I use:
from ToolSet import ToolSet
...
toolSet = ToolSet()
instead:
import ToolSet
...
toolSet = ToolSet.ToolSet()
I do this to reduce stuttering (I prefer to stutter at the top of the file than in my code.)
Is this the correct idiom?
Here is a related question. Inside the package, I often have a small number of classes that I would like to expose to the outside world. They are imported internally __init__.pyfor this package. For example, if it ToolSetis in a package UIand I want to expose it, I would put the following in UI/__init__.py:
from ToolSet import ToolSet
So, from the external module I can write
import UI
...
toolSet = UI.ToolSet()
Again, is this pythonic?
source
share