472,353 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How do I access a main frunction from an import module?

Jim
Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
######################

python25.exe main.py

Thanks,
Jim

Nov 24 '06 #1
11 1397
Jim wrote:
Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
######################

python25.exe main.py
import __main__
....
__main__.m()
but better make a start.py and "import main" - then its symmetric
Robert
Nov 24 '06 #2

Jim wrote:
I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
######################

You can do it with "from __main__ import m" atop abc.py (the module
invoked at the command line is always called __main__).

However, I *highly* suggest you put m in another file. Importing
variables from __main__ would make your program incompatible with many
useful tools. For example, if you invoke the Python profiler on your
code, like this:

python -m profile main.py

it will break your code, because __main__ no longer refers to main.py
but to profile.py (from the Python library). Importing from __main__
adversely affects tools such as PyChecker and PyLint.

The exception to this would be if abc.py is specifically designed as a
utility for interactive use; then it would be ok and useful.
Carl Banks

Nov 24 '06 #3
Jim wrote:
I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
import sys
def a():
sys.modules['__main__'].m()
return None

Anton

'now why would anyone want to do *that* ?'
Nov 24 '06 #4
Jim wrote:
I have created an import module. And would like to access a
function from the main script, e.g.,
May I ask why? This style violates "normal" module philosophy.

Regards,
Björn

--
BOFH excuse #307:

emissions from GSM-phones

Nov 24 '06 #5
Jim

Bjoern Schliessmann wrote:
Jim wrote:
I have created an import module. And would like to access a
function from the main script, e.g.,

May I ask why? This style violates "normal" module philosophy.

Regards,
Björn

--
BOFH excuse #307:

emissions from GSM-phones
Application abc is designed as a complete module. The user is to
script their own functions to work with application abc.

Thanks,
JIm

Nov 24 '06 #6
Jim wrote:
Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
######################

python25.exe main.py
Although there are literally correct answers to your question, the best
answer is "Don't do that. You would be creating circular references
between modules, and run the risk of emulating the mythical ooloo bird
by disappearing up your own fundamental orifice". Some possible
practical solutions:

1. Put the m function in a 3rd file/module. Then any other module which
needs it can import/call.

2. If you think that's not a good idea, then put it in abc.py (it's not
used in main.py in your example).

3. Maybe this will suit what you are really trying to do:

file abc.py:
###################
def a(argfunc): # <<<<<=====
argfunc() # <<<<<=====
####################

file main.py:
#####################
from abc import *
def m():
print 'something'

a(m) # <<<<<=====
######################

4. If you think *that's* not a good idea, then you might like to
explain at a higher level what you are *really* trying to achieve :-)
E.g. "Function m is one of n functions in main.py of which abc.py
may/must call 0, 1, or many because blah blah blah ..."

BTW, "return None" at the very end of a function is redundant. The
Python compiler generates "return None" automagically (implicitly!?)
instead of letting you fall off the end of the world. Which book or
tutorial are you using?

BTW #2: "python25.exe main.py" ?? If you are on Windows, have Python
2.4 as your default setup, and are trialling 2.5: you may like to ask
(in a new thread) about more convenient ways of doing it. Otherwise you
might like to tell what you are up to (in a new thread) so that your
problem can be diagnosed correctly and cured :-)

HTH,
John

Nov 24 '06 #7
Jim wrote:
Application abc is designed as a complete module. The user is to
script their own functions to work with application abc.
so use execfile() with a prepared namespace:

namespace = { ...stuff to export to the module ... }
execfile("directory/module.py", namespace)

</F>

Nov 24 '06 #8
Jim
John Machin wrote:
Jim wrote:
Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print 'something'
return None

a()
######################

python25.exe main.py

Although there are literally correct answers to your question, the best
answer is "Don't do that. You would be creating circular references
between modules, and run the risk of emulating the mythical ooloo bird
by disappearing up your own fundamental orifice". Some possible
practical solutions:

1. Put the m function in a 3rd file/module. Then any other module which
needs it can import/call.

2. If you think that's not a good idea, then put it in abc.py (it's not
used in main.py in your example).

3. Maybe this will suit what you are really trying to do:

file abc.py:
###################
def a(argfunc): # <<<<<=====
argfunc() # <<<<<=====
####################

file main.py:
#####################
from abc import *
def m():
print 'something'

a(m) # <<<<<=====
######################

4. If you think *that's* not a good idea, then you might like to
explain at a higher level what you are *really* trying to achieve :-)
E.g. "Function m is one of n functions in main.py of which abc.py
may/must call 0, 1, or many because blah blah blah ..."

BTW, "return None" at the very end of a function is redundant. The
Python compiler generates "return None" automagically (implicitly!?)
instead of letting you fall off the end of the world. Which book or
tutorial are you using?

BTW #2: "python25.exe main.py" ?? If you are on Windows, have Python
2.4 as your default setup, and are trialling 2.5: you may like to ask
(in a new thread) about more convenient ways of doing it. Otherwise you
might like to tell what you are up to (in a new thread) so that your
problem can be diagnosed correctly and cured :-)

HTH,
John
BTW#1: I have most of the python books from O'Reilly. I'm sure that
some of them say that its a good idea to use 'return None'. However,
most their examples do not us it. Anyway I find that its useful when
reading my own scripts.

BTW#2: I do not have Python 2.4 installed anymore. Therefore, it is
not a trialling problem.

I thought that it would be NICE to keep the application and the user's
script separate from each other, being that python is so flexible.
However, there seems to be no end to the problems that occur by doing
this. So I will abandon this exercise, since it appears not to be a
very good programming practise.

Thanks,
Jim

Nov 24 '06 #9
This is an interesting question. It almost looks like a case of
event-driven programming, where main is the plug-in and abc is the
framework.
http://eventdrivenpgm.sourceforge.net/

So how about something like this:

################## abc.py ####################

#------------------------------------------------------------
# an "abstract" function.
# It should be over-ridden in the calling program
#------------------------------------------------------------
def m():
raise AssertionError("You should have over-ridden abstract function
m()")

def a():
m()
return None
########### main.py ####################
import abc # "instantiate" the framework

# define our our "concrete" function m
def m():
print 'something'
return None

#-----------------------------------------------
# override the "abstract" function abc.m()
# with our own "concrete" function m().
# Comment out this line and see what happens.
#-----------------------------------------------
abc.m = m

# invoke the a() function in the abc framework
abc.a()

#################################################

Nov 24 '06 #10

Jim wrote:
John Machin wrote:
Jim wrote:
Hi,
>
I have created an import module. And would like to access a function
from the main script, e.g.,
>
file abc.py:
###################
def a():
m()
return None
####################
>
file main.py:
#####################
from abc import *
def m():
print 'something'
return None
>
a()
######################
>
python25.exe main.py
>
Although there are literally correct answers to your question, the best
answer is "Don't do that. You would be creating circular references
between modules, and run the risk of emulating the mythical ooloo bird
by disappearing up your own fundamental orifice". Some possible
practical solutions:

1. Put the m function in a 3rd file/module. Then any other module which
needs it can import/call.

2. If you think that's not a good idea, then put it in abc.py (it's not
used in main.py in your example).

3. Maybe this will suit what you are really trying to do:

file abc.py:
###################
def a(argfunc): # <<<<<=====
argfunc() # <<<<<=====
####################

file main.py:
#####################
from abc import *
def m():
print 'something'

a(m) # <<<<<=====
######################

4. If you think *that's* not a good idea, then you might like to
explain at a higher level what you are *really* trying to achieve :-)
E.g. "Function m is one of n functions in main.py of which abc.py
may/must call 0, 1, or many because blah blah blah ..."

BTW, "return None" at the very end of a function is redundant. The
Python compiler generates "return None" automagically (implicitly!?)
instead of letting you fall off the end of the world. Which book or
tutorial are you using?

BTW #2: "python25.exe main.py" ?? If you are on Windows, have Python
2.4 as your default setup, and are trialling 2.5: you may like to ask
(in a new thread) about more convenient ways of doing it. Otherwise you
might like to tell what you are up to (in a new thread) so that your
problem can be diagnosed correctly and cured :-)

HTH,
John

BTW#1: I have most of the python books from O'Reilly. I'm sure that
some of them say that its a good idea to use 'return None'.
Instead of "return None", consider using "return for refund" ;-)
However,
most their examples do not us it. Anyway I find that its useful when
reading my own scripts.

BTW#2: I do not have Python 2.4 installed anymore. Therefore, it is
not a trialling problem.
Looks like it *was* a trialling problem, with weird residual effects.
The point was that it's a very strange practice to (a) name the
executable "python25.exe" [standard installation would produce
C:\Python25\python.exe] (b) want/need to use ".exe" when invoking it.
>
I thought that it would be NICE to keep the application and the user's
script separate from each other, being that python is so flexible.
Most other people think that that's a very nice idea too; there was
just no clue in your original posting about what you were really trying
to do.
However, there seems to be no end to the problems that occur by doing
this. So I will abandon this exercise, since it appears not to be a
very good programming practise.
So what about my suggestion 3? I got the impression from your reply to
Bjoern that it was a good fit for your case. The functions are in what
we now know to be the user's script, and the app calls them. What
problems?

Cheers,
John

Nov 24 '06 #11
Jim

Steve wrote:
This is an interesting question. It almost looks like a case of
event-driven programming, where main is the plug-in and abc is the
framework.
http://eventdrivenpgm.sourceforge.net/

So how about something like this:

################## abc.py ####################

#------------------------------------------------------------
# an "abstract" function.
# It should be over-ridden in the calling program
#------------------------------------------------------------
def m():
raise AssertionError("You should have over-ridden abstract function
m()")

def a():
m()
return None
########### main.py ####################
import abc # "instantiate" the framework

# define our our "concrete" function m
def m():
print 'something'
return None

#-----------------------------------------------
# override the "abstract" function abc.m()
# with our own "concrete" function m().
# Comment out this line and see what happens.
#-----------------------------------------------
abc.m = m

# invoke the a() function in the abc framework
abc.a()

#################################################
Thank you Steve.

You are correct, the project that I'm working on is an event drive
program. And your solution works perfectly for this simple example.
My poroject does actually have what you called an "abstract" function.
Now I will know try to implement it into my project.

Thanks again.
Jim

Nov 25 '06 #12

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

Similar topics

2
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden,...
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to...
2
by: enrio | last post by:
I need to process the source code associated with the forms outside access, and import the changes back to Access. I find that I can export the...
2
by: kkrizl | last post by:
I have an Access database that has a few huge tables. It was taking about 20 minutes per table to import them from another application. I used...
14
by: Greg Copeland | last post by:
I am running python on VxWorks. In the course of operation, a vxworks tasks writes to a reserved area of memory. I need access to this chunk of...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've...
6
by: fatwallet961 | last post by:
is the main function in python is exact compare to Java main method? all execution start in main which may takes arguments? like the follow...
8
by: Sullivan WxPyQtKinter | last post by:
I am confused by the following program: def f(): print x x=12345 f() result is: 12345
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.