[wingide-users] global imports

Mclam, GeorgeX georgex.mclam at intel.com
Tue Jun 7 09:56:25 MDT 2011


Hello Paul and thank you for your reply.

> The question seems more appropriate for python-list

I think you are correct.  Can you direct me to that list?


> Like you say, give your subroutines module an init function:

> # project_main.py
> import parameter1, parameter2, parameter3
> import subroutines
> subroutines.init(parameter1, parameter2, parameter3)

Yup, did that.  No problem in that aspect.  The problem is that items imported into the init routine are local to that routine.  I need them to be global to the entire subroutines module.


g> I'd have to handle each and every value being imported to make it global in the module.

> No; just save off the modules:

> # subroutines.py
> def init(a,b,c):
>    global ga, gb, gc
>    ga,gb,gc = a,b,c

One of us is not understanding the other.
Virtually EVERY PARAMETER of the code is needed by the subroutines module.  If I was able to do something like:

# subroutines.py
    def init(module_name)
    global *
    import module_name

then it would work.   Otherwise I have to list (in global statements) each and every parameter that is being imported for subroutines to use.  That's totally impractical.  Again, I do not intend to have subroutines modify any of these parameters, just access them.



> Alternatively, since you say "it's quite practical to pass the names of
> the modules for a project (3 files)", instead of passing names pass
> modules.

> subroutines.do_something(parameter1, parameter2, parameter3, ...)

What I was referring to is passing the names of the modules to subroutines so it could import them.  And I can do that, but it is from a function.  The problem is making all that is imported global to the module as if the imports were at the head of the module.  As you know, when imported in a function, the parameters are only available to THAT function.


G> Importing in each function of the subroutines module makes no sense
G> and creating separate copy of the subroutines module just so the import
G> statements at the top have the proper hard-coded project module names
G> makes no sense either.

In the first case I was considering having to put import statements in each function of subroutines.py so that they could gain access to the parameters in lieu of doing it once at the top of the module.

In the second case I was considering having my application write out a "header" with the import statements, then concatenating the subroutines module into a new module to be included and called.  It provides a means to change the otherwise hard-coded arguments to the import statements and have all that is imported by the subroutines module be global to all functions in the module.

But neither of the above makes better sense than what I am presently doing.


Thanks,
George


-----Original Message-----
From: wingide-users-bounces at wingware.com [mailto:wingide-users-bounces at wingware.com] On Behalf Of Paul Du Bois
Sent: Monday, June 06, 2011 5:55 PM
To: wingide-users at wingware.com
Subject: RE: [wingide-users] global imports

The question seems more appropriate for python-list.  However, to try to
answer the question: modules can be passed around just as easily as
anything else.  Like you say, give your subroutines module an init
function:

# project_main.py
import parameter1, parameter2, parameter3
import subroutines
subroutines.init(parameter1, parameter2, parameter3)

> I'd have to handle each and every value being imported to make it
global in the module.

No; just save off the modules:

# subroutines.py
def init(a,b,c):
    global ga, gb, gc
    ga,gb,gc = a,b,c


Alternatively, since you say "it's quite practical to pass the names of
the modules for a project (3 files)", instead of passing names pass
modules.

subroutines.do_something(parameter1, parameter2, parameter3, ...)


Caveat: I may not have understood your problem correctly.  In
particular, this bit made no sense to me:

> Importing in each function of the subroutines module makes no sense
and creating separate copy of the subroutines module just so the import
statements at the top have the proper hard-coded project module names
makes no sense either.

p
_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide


More information about the wingide-users mailing list