472,334 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How to pass parameter to a module?

I'd like to parametrize a module. That is, to set and pass
some values into the module, while it is being imported.
I tried this:

# sub_1.py -- main program
extern = "OK"
import sub_2
print sub_2.noway # prints 'no extern' :-(
# EOF

# sub_2.py -- parametrized module, parameter is the 'extern' var
try:
noway = extern
except:
noway = 'no extern'
# EOF

You can guess, it doesn't work.
How can I pass the parameter? Can I?
M-a-S
Jul 18 '05 #1
7 18287
M-a-S wrote:
I'd like to parametrize a module. That is, to set and pass
some values into the module, while it is being imported.
I tried this:

# sub_1.py -- main program
extern = "OK"
import sub_2
print sub_2.noway # prints 'no extern' :-(
# EOF

# sub_2.py -- parametrized module, parameter is the 'extern' var
try:
noway = extern
except:
noway = 'no extern'
# EOF

You can guess, it doesn't work.
How can I pass the parameter? Can I?


Create an additional module sub_2_parameters:
# sub_1.py -- main program
import sub_2_parameters
sub_2_parameters.extern = "OK"
import sub_2

# sub_2.py -- parametrized module, parameter is the 'extern' var
import sub_2_parameters
try:
noway = sub_2_parameters.extern
except:
noway = 'no extern'
# EOF

Note that modules are imported only once so you cannot import sub_2 with one
set of parameters into one module and with another set of variables into
another.

Depending on your problem, it might be better to use the builtin execfile,
because you can pass a dictionary with the parameters to the call.

Daniel

Jul 18 '05 #2
"M-a-S" <NO*****@hotmail.com> schrieb im Newsbeitrag
news:Vw*****************@twister.southeast.rr.com. ..
I'd like to parametrize a module. That is, to set and pass
some values into the module, while it is being imported.
I tried this:

# sub_1.py -- main program
extern = "OK"
import sub_2
print sub_2.noway # prints 'no extern' :-(
# EOF

# sub_2.py -- parametrized module, parameter is the 'extern' var
try:
noway = extern
except:
noway = 'no extern'
# EOF


It looks like you are using modules where you would rather use functions.
exp:
-------------------cut---------
def sub2(blah=None):
if blah is None:
return "no extern"
else:
return blah

sub2('test')
sub2()

HTH

Ciao Ulrich
Jul 18 '05 #3
I thought about the third module. It doesn't sound good.
I hoped there're some tricks with __dict__, frames and
other __...__ objects.

For now, this is what does the task:

# sub_2.py
''' The documentation will look like this:
To set the size of dictionary and search depth,
do before including (default values are shown):
import string
string.sub_2_dictionary_size = 10 # in megabytes
string.sub_2_search_depth = 1000 # in nodes
string.sub_2_max_responce_time = 100 # seconds
'''
import string # it's used in sub_2 anyway
# parameters
try: _DICT_SZ = string.sub_2_dictionary_size
except: _DICT_SZ = 10
try: _DEPTH = string.sub_2_search_depth
except: _DEPTH = 1000
try: _RTIME = string.sub_2_max_responce_time
except: _RTIME = 100
# now the stuff
def do_the_job():
return "Processing with dict=%d depth=%d rtime=%d" % (_DICT_SZ,_DEPTH,_RTIME)
# EOF

# sub_1.py
import string # actually, some module, which is used in sub_2
string.sub_2_dictionary_size = 20 # megabytes
string.sub_2_search_depth = 5000 # nodes
# let's leave sub_2_max_responce_time default
import sub_2
print sub_2.do_the_job()
# EOF

M-a-S

"Daniel Dittmar" <da************@sap.com> wrote in message news:bk**********@news1.wdf.sap-ag.de...
M-a-S wrote:
I'd like to parametrize a module. That is, to set and pass


Create an additional module sub_2_parameters:
# sub_1.py -- main program
import sub_2_parameters
sub_2_parameters.extern = "OK"
import sub_2

# sub_2.py -- parametrized module, parameter is the 'extern' var
import sub_2_parameters
try:
noway = sub_2_parameters.extern
except:
noway = 'no extern'
# EOF

Note that modules are imported only once so you cannot import sub_2 with one
set of parameters into one module and with another set of variables into
another.

Depending on your problem, it might be better to use the builtin execfile,
because you can pass a dictionary with the parameters to the call.

Daniel

Jul 18 '05 #4

"M-a-S" <NO*****@hotmail.com>
I thought about the third module. It doesn't sound good.
I hoped there're some tricks with __dict__, frames and
other __...__ objects.


Well, there are some tricks ;-)
myVariable='great surprise'
import x
........

"This is modul X"
import sys
print sys.modules['__main__'].myVariable
But generally the namespace of a module is ... the module.

Kindly
Michael P
Jul 18 '05 #5
That's what I asked. Thanks!
M-a-S

"Michael Peuser" <mp*****@web.de> wrote in message news:bk*************@news.t-online.com...

"M-a-S" <NO*****@hotmail.com>
I thought about the third module. It doesn't sound good.
I hoped there're some tricks with __dict__, frames and
other __...__ objects.


Well, there are some tricks ;-)
myVariable='great surprise'
import x
.......

"This is modul X"
import sys
print sys.modules['__main__'].myVariable
But generally the namespace of a module is ... the module.

Kindly
Michael P

Jul 18 '05 #6
"M-a-S" <NO*****@hotmail.com> wrote in message news:<Vw*****************@twister.southeast.rr.com >...
I'd like to parametrize a module. That is, to set and pass
some values into the module, while it is being imported.


There are many ways.

(However, classes may fit your need better. "Passing parameters to a
module" is not a common practice, as far as I know.)

(1) Use a built-in namespace variable.

import __builtin__
__builtin__.myvar = 3
print myvar

This is a hack. I have been screamed at for mentioning it. :)

(2) Hack your favorite (non-builtin) module. The module could be any
of the standard library modules, or your own third module.

import sys
sys.myvar = 3

(3) Use environmental variables.

import os
os.environ['myvar'] = 'hello'

You get the idea. Python has three namespaces: built-in, global, and
local. Since global and local namespaces won't go over to the other
module, you need to rely on the built-in namespace, one way or
another. How you want to structure your data (by using the built-in
namespace, a module, a class, a dictionary or any other entities that
can hold a name entry), it's entirely up to you.

Hung Jung
Jul 18 '05 #7
Thank you!

"Hung Jung Lu" <hu********@yahoo.com> wrote in message news:8e**************************@posting.google.c om...
"M-a-S" <NO*****@hotmail.com> wrote in message news:<Vw*****************@twister.southeast.rr.com >...
I'd like to parametrize a module. That is, to set and pass
some values into the module, while it is being imported.


There are many ways.

(However, classes may fit your need better. "Passing parameters to a
module" is not a common practice, as far as I know.)

(1) Use a built-in namespace variable.

import __builtin__
__builtin__.myvar = 3
print myvar

This is a hack. I have been screamed at for mentioning it. :)

(2) Hack your favorite (non-builtin) module. The module could be any
of the standard library modules, or your own third module.

import sys
sys.myvar = 3

(3) Use environmental variables.

import os
os.environ['myvar'] = 'hello'

You get the idea. Python has three namespaces: built-in, global, and
local. Since global and local namespaces won't go over to the other
module, you need to rely on the built-in namespace, one way or
another. How you want to structure your data (by using the built-in
namespace, a module, a class, a dictionary or any other entities that
can hold a name entry), it's entirely up to you.

Hung Jung

Jul 18 '05 #8

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a...
4
by: Bo Peng | last post by:
Dear list, What I would like to do is something like: In myModule.py ( a wrapper module for different versions of the module), if lib ==...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: ...
4
by: deko | last post by:
This is a basic program flow question. I'm trying to refractor an AC2000 app and split sections of code into separate modules. But there are a...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call...
6
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call...
1
by: =?Utf-8?B?RGFtaXIgRGV6ZWxqaW4=?= | last post by:
Hi. I have to implement an Java Axis2 web service client in .NET WCF as well a web service to witch a Java Axis2 client will connect. I would...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor,...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
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...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
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...

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.