views:

54

answers:

2

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?

+2  A: 

I'm not really sure what your problem is, is it that you just want to type less?

  • get a decent source editor with autocomplete!
  • you can do import longmodulename as ln and use ln.something instead of longmodulename.something
  • you can do from longmodulename import ( something, otherthing ) and use something directly

import * is never a good idea, it messes with coding tools, breaks silently, makes readers wonder stuff was defined and so on ...

THC4k
How do you know if the whole module is being imported? Is there some sort of self that refers back to the current module?
CodeJoust
There is no (reasonable) way to know how a module was imported and there is no reason why anyone would need to know that. What are you trying o do?
THC4k
+3  A: 

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.

joefis