__call__ in module?

N

ncf

I have a feeling that this is highly unlikely, but does anyone in here
know if it's possible to directly call a module, or will I have to wrap
it up in a class?

i.e.,
import MyMod
MyMod.whatever = "Hi?"
MyMod("meow mix")


Thanks in advance
-Wes
 
Q

Qopit

Nope - you can't even force it by binding a __call__ method to the
module.

For future reference, you can check to see what things *are* callable
with the built-in function 'callable'.

eg (with sys instead of MyApp):
False

Also - a thing you can do to sort of do what you want (?) is wrap the
code to be executed by the module in a main() function. eg:

#-- Start of MyApp.py --
def main(foo):
print "My cat loves", foo

if __name__ == "__main__":
import sys
main(" ".join(sys.argv[1:]))
#-- EOF --

The main function then lets you either run your module from the command
line (MyApp.py Meow Mix) or have another module use it with:

import MyApp
MyApp.main("Meow Mix")
 
L

Leif K-Brooks

ncf said:
I have a feeling that this is highly unlikely, but does anyone in here
know if it's possible to directly call a module, or will I have to wrap
it up in a class?

You could use trickery with sys.modules to automatically wrap it in a class:

import sys
from types import ModuleType

class CallableModule(ModuleType):
def __call__(self):
print "You called me!"

mod = FooModule(__name__, __doc__)
mod.__dict__.update(globals())
sys.modules[__name__] = mod
 
S

Steven D'Aprano

I have a feeling that this is highly unlikely, but does anyone in here
know if it's possible to directly call a module, or will I have to wrap
it up in a class?

Why not try it yourself? Write a quick module like this:

"""Call a module"""

def __call__():
return "Thank you for calling."

then try to call it from an interactive session:

py> import caller
py> caller()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'module' object is not callable

With things this simple, you learn more from doing than from asking.


A better question is, if a module object has a __call__ method, shouldn't
it *be* callable? That would let you write a script, put the normal Python
idiom at the end:

if __name__ == "__main__":
__call__(sys.argv) # instead of "main()"

No major benefit there, we've just changed main to __call__. But
then you can call the script as a stand-alone piece of code from within
Python:

import script
result = script(my_args)
 
N

ncf

My thanks to you and Fredrik Lundh for the quality replies. however
much so, I appreciate your moreso descriptive reply.

I probably should've been a little more specific in my original query,
and have stated that I *did* try it before I posted here asking for
help. I was just hoping somebody would be able to prove my test wrong.

Oh well, have a good day
-Wes
 
N

ncf

My thanks to you and Fredrik Lundh for the quality replies. however
much so, I appreciate your moreso descriptive reply.

I probably should've been a little more specific in my original query,
and have stated that I *did* try it before I posted here asking for
help. I was just hoping somebody would be able to prove my test wrong.

Oh well, have a good day
-Wes
 
N

ncf

Thanks for this information. It'd really be interesting to see how well
this works for the code I wish to apply it to.

Thanks again and have a GREAT day.
-Wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top