473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18406
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_parameter s:
# sub_1.py -- main program
import sub_2_parameter s
sub_2_parameter s.extern = "OK"
import sub_2

# sub_2.py -- parametrized module, parameter is the 'extern' var
import sub_2_parameter s
try:
noway = sub_2_parameter s.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*****@hotmai l.com> schrieb im Newsbeitrag
news:Vw******** *********@twist er.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_di ctionary_size = 10 # in megabytes
string.sub_2_se arch_depth = 1000 # in nodes
string.sub_2_ma x_responce_time = 100 # seconds
'''
import string # it's used in sub_2 anyway
# parameters
try: _DICT_SZ = string.sub_2_di ctionary_size
except: _DICT_SZ = 10
try: _DEPTH = string.sub_2_se arch_depth
except: _DEPTH = 1000
try: _RTIME = string.sub_2_ma x_responce_time
except: _RTIME = 100
# now the stuff
def do_the_job():
return "Processing with dict=%d depth=%d rtime=%d" % (_DICT_SZ,_DEPT H,_RTIME)
# EOF

# sub_1.py
import string # actually, some module, which is used in sub_2
string.sub_2_di ctionary_size = 20 # megabytes
string.sub_2_se arch_depth = 5000 # nodes
# let's leave sub_2_max_respo nce_time default
import sub_2
print sub_2.do_the_jo b()
# EOF

M-a-S

"Daniel Dittmar" <da************ @sap.com> wrote in message news:bk******** **@news1.wdf.sa p-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_parameter s:
# sub_1.py -- main program
import sub_2_parameter s
sub_2_parameter s.extern = "OK"
import sub_2

# sub_2.py -- parametrized module, parameter is the 'extern' var
import sub_2_parameter s
try:
noway = sub_2_parameter s.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*****@hotmai l.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='gre at 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*****@hotmai l.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='gre at 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*****@hotmai l.com> wrote in message news:<Vw******* **********@twis ter.southeast.r r.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__.myv ar = 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********@yah oo.com> wrote in message news:8e******** *************** ***@posting.goo gle.com...
"M-a-S" <NO*****@hotmai l.com> wrote in message news:<Vw******* **********@twis ter.southeast.r r.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__.myv ar = 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
3530
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 large list or dictionary? It could achieved by pointer in C++, is there such way in Python? Thansk in advance. J.R.
4
8763
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 == 'standard': from myModule_std import * elsif lib == 'optimized' from myModule_op import *
5
11071
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: Private Sub Report_Open(Cancel As Integer) modHandler.rptOpen (Me.Report.Name), (Me.Tag) End Sub
4
11991
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 number of collections I create in one big module - containing file paths, Excel worksheet names, etc - I'm not sure how to pass these around, or if I'd be better off saving things to a table and using a recordset. My guess is a recordset would be slower. For example, should I do something like...
3
5491
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 certain methods in each class and get back an array of data. All four classes are the same except for the number of elements in their arrays and the data. I have a MainClass (the form), which processes all of these arrays. The MainClass makes use of third party objects to do this. There...
6
2407
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 project was written in VB. I have converted it to C#. One of the problem is passing a function address to a COM function as a parameter with another progress value. My callback function is very simple using the progress value to update my progressbar. Because this COM function usually takes a long...
9
5015
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 methods of that object's class. I tried something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
1
3129
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 like to provide user identification using basicHTTP user/pass authentication. Later I'll move both ends to SSL (https). I already implemented the Java Axis2 end: - Axis2 Client axis2.xml:
12
11115
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.