Reputation: 3790
Currently, I have a parser with multiple classes that work together.
For Instance: TreeParser creates multiple Product and Reactant modules which in turn create multiple Element classes. The TreeParser is called by a render method within the same module, which is called from the importer.
Finally, if the package has dependencies (such as re and another another module within the same folder), where is the best place to require those modules? Within the __init__.py
file or within the module itself?
EDIT:
When importing a part of a module that calls another def within the module, how do you call that def if it isn't imported?
lib/toolset.py => def add(){ toolset.show("I'm Add"); } def show(text){print text};
if that file is called from main.py => import lib.toolset
then, the show method wouldn't be loaded, or main.py => from lib.toolset import show
wouldn't work.
Can an import toolset
be put at the top of toolset.py
?
Upvotes: 1
Views: 205
Reputation: 1211
I think this is the key statement in your question.
I don't really want to add the module name in front of every call to the class
My response: I hear what you're saying, but this is standard practice in Python.
Any Python programmer reading code like "result = match(blah)" will presume you're calling a local function inside your own module. If you're actually talking about the function match() in the re module they'll expect to see "result = re.match(blah)". That's just how it is.
If it helps, I didn't like this style either when I came to Python first, but now I appreciate that it removes any ambiguity over exactly which of the many functions called "match" I am calling, especially when I come back to read code that I wrote six months ago.
Upvotes: 3
Reputation: 107598
I'm not really sure what your problem is, is it that you just want to type less?
import longmodulename as ln
and use ln.something
instead of longmodulename.something
from longmodulename import ( something, otherthing )
and use something
directlyimport *
is never a good idea, it messes with coding tools, breaks silently, makes readers wonder stuff was defined and so on ...
Upvotes: 2