473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

__call__ in module?

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

Sep 27 '05 #1
7 1761
"ncf" wrote.
I have a feeling that this is highly unlikely, but does anyone in here
know if it's possible to directly call a module


no.

</F>

Sep 27 '05 #2
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):
import sys
callable(sys)

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")

Sep 27 '05 #3
On Tue, 27 Sep 2005 14:19:13 -0700, ncf wrote:
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)
--
Steven.

Sep 27 '05 #4
ncf wrote:
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
Sep 27 '05 #5
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

Sep 28 '05 #6
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

Sep 28 '05 #7
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

Sep 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
11
by: Stefan Behnel | last post by:
Hi! This somewhat puzzles me: Python 2.4 (#1, Feb 3 2005, 16:47:05) on linux2 Type "help", "copyright", "credits" or "license" for more information. ..>>> class test(object): .... def...
8
by: Rahul | last post by:
Consider the following: def a(x): return x+1 def b(f): def g(*args,**kwargs): for arg in args: print arg return f(*args,**kwargs) return g
5
by: Kent Johnson | last post by:
I am learning about metaclasses and there is something that confuses me. I understand that if I define a __call__ method for a class, then instances of the class become callable using function...
7
by: Gigs_ | last post by:
from Tkinter import * from tkFileDialog import askopenfilename from tkColorChooser import askcolor from tkMessageBox import askquestion, showerror from tkSimpleDialog import askfloat demos...
17
by: skip | last post by:
I don't personally use __call__ methods in my classes, but I have encountered it every now and then here at work in code written by other people. The other day I replaced __call__ with a more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.