1

The documentation for __init__.py is quite hard to find. I can't find a place that explains all the things you can do in this file. The Python module documentation barely even mentions __init__.py nor that you can use __all__ for from module import *

What I want is for my module to be callable like:

main.py

import module

module()

module/__ init __.py

def __call__(self):    # self here cause modules are loaded as objects?
    print 'callable'
WobbaFetttttt
  • 53
  • 2
  • 8

1 Answers1

1

Maybe silly answer but you can add method into __init__.py file like

__init__.py

def module():
    # what functions do you need to run into __init__ file

and then

main.py

from module import module
module()

also, you just can write some operations into init and then it will be calling after import

example:

__init__.py

print('1')
print('2')
print('3')

main.py

import module

after run main.py the output will be

1
2
3

but actually, it's not good practice. Try to write code without "calling" modules because a module is a file containing Python definitions and statements, not the function that needs to call.

Artem Kryvonis
  • 128
  • 2
  • 13
  • yea thanks i am aware of this, there plenty of functions in my `module`, but for esthetic purposes i wanted my module to be callable like a bunch of other objects my code deal with – WobbaFetttttt Feb 15 '17 at 11:33
  • did you see this [question](http://stackoverflow.com/questions/1060796/callable-modules) maybe will be useful for you. – Artem Kryvonis Feb 15 '17 at 11:45
  • OH MY BOY, you mah boy artem, thanks so much. wish i have enough rep to give you an up too <3 – WobbaFetttttt Feb 15 '17 at 11:57