0

Apologies if this has been asked before, I'm not sure how to search for this.

I'm trying to structure my module in a way that allows me to do this:

import module
module()

How can I do this?

My module is a file parser, and right now I'm doing this:

from module import module

with open('file') as f:
    parsed_file = module(f)

Importing the same name as the module feels redundant.

nathancahill
  • 10,452
  • 9
  • 51
  • 91
  • 3
    What is `module()` supposed to do` Any code directly contained in the module is already executed when you import it. To give you more control, it is good practice to create an `if __name__ == '__main':` block at the end, calling some sort of `main()` function. If you do this, just do `module.main()`. – tobias_k Jul 17 '14 at 15:49
  • 1
    Any problem with `from module import function` `function()` ? – joaoricardo000 Jul 17 '14 at 15:50
  • do you mean use `function()` instead of `module.function()`? If this is what you want, you can do that by `from module import *` – RealityPC Jul 17 '14 at 15:51
  • @tobias_k Added clarification in the question. – nathancahill Jul 17 '14 at 15:59

2 Answers2

0

In my experience I just do from my_module import function_in_module

Then you can use function_in_module() I haven't had a module that I could just module() because that just tells you to look in that source unless you have a function that matches the module's name.

Haven't really learn how to do just import my_module

http://www.tutorialspoint.com/python/python_modules.htm

This should help.

0

Is module a part of the module? You can always do

from module import *

if you don't feel like writing module twice. Keep in mind though this will import everything from module, which you may or may not want.

jj172
  • 751
  • 2
  • 9
  • 35