how to pass parameters when calling a module. I have one module.py and i need to pass variables as arguments for the same module, so that I can use that variable values for processing( like in unix shell scripting). Is ther any way to handle the same ? pls advise
| Python |
1 |
|


Comments
Are you sure? You can put globals into a third module. I often call this module "Globals.py".
However, argument-passing seems unwise. If module A imports module B, there is no guarantee that module B will use the "arguments" passed to it from A. For example:
The problem is that B could be imported before A, so A does not really pass anything at all to B.
This isn't a newgroup... ...for posting questions, but I'll try answering.
If by "when calling a module" you mean as one is imported, there's no direct way to do it that I know of. You could do so indirectly by first writing information out to a file, and then reading it in the module's body (which only executes the first time it's imported).
A slightly more direct approach for passing some parameters would be to use the fact that Python stores entries to all modules imported in the "sys.modules" dictionary however it doesn't force the values associated with the entries to be module objects -- leaving you free to install instances your own objects there which do what you want.
Here's an illustration of what I'm saying:
[Output from sample.py]
Ahem... Never mind. My sample code doesn't accomplish anything that couldn't be done in a normal module. The general idea can be useful for such things as controlling access to a module attributes via the __setattr__ and __getattr__ object methods -- which regular module objects don't possess -- as shown in recipe 6.2 in the 2nd edition of the "Python Cookbook". However it doesn't help here.
Ahem... Never mind my previous comment. The sample code doesn't actually accomplish anything that couldn't be done with a regular module. The technique described can be useful, as shown in recipe 6.2 titled "Define Constants" in the 2nd edition of the "Python Cookbook". However, unlike the case there, the class defined in my example doesn't take advantage of any special object attributes or methods not possessed by regular module objects.
Sign in to comment