ActiveState Code

Recipe 499300: how to pass parameters when a calling module


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

  1. 1. At 7:48 p.m. on 5 jan 2007, Christopher Dunn said:

    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:

    [Globals.py]
    name = 'Perl'
    
    [B.py]
    import Globals
    print "Using 'name'", Globals.name
    
    [A.py]
    import Globals
    Globals.name = 'Python'
    import B
    
    [main.py]
    import Globals
    Globals.name = 'Ruby'
    import B
    import A
    
    Result of main.py: Using 'name' Ruby.
    

    The problem is that B could be imported before A, so A does not really pass anything at all to B.

  2. 2. At 6:29 p.m. on 7 jan 2007, Martin Miller said:

    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:

    #### file module.py
    
    class _mymodule(object):
        def __init__(self):
            self.a = 'a'
            self.b = 'b'
            self.variables = None
    
        def pass_variables(self, *variables):
            self.variables = variables
    
    import sys
    sys.modules[__name__] = _mymodule()
    
    #### end file module.py
    
    #### file sample.y
    
    import module
    module.pass_variables(42,'string')
    
    print "module.a:", module.a
    print "module.b:", module.b
    print "module.variables:", module.variables
    
    #### end file sample.y
    

    [Output from sample.py]

    module.a: a
    module.b: b
    module.variables: (42, 'string')
    
  3. 3. At 6:58 p.m. on 7 jan 2007, Martin Miller said:

    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.

  4. 4. At 7:15 p.m. on 7 jan 2007, Martin Miller said:

    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