How to pass parameter to a module?

Discussion in 'Python' started by M-a-S, Sep 18, 2003.

  1. M-a-S

    M-a-S Guest

    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
    M-a-S, Sep 18, 2003
    #1
    1. Advertising

  2. 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
    Daniel Dittmar, Sep 18, 2003
    #2
    1. Advertising

  3. M-a-S

    Ulrich Petri Guest

    "M-a-S" <> schrieb im Newsbeitrag
    news:Vwfab.2454$...
    > 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
    Ulrich Petri, Sep 18, 2003
    #3
  4. M-a-S

    M-a-S Guest

    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" <> wrote in message news:bkc62g$5k0$-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
    M-a-S, Sep 18, 2003
    #4
  5. "M-a-S" <>

    > 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
    Michael Peuser, Sep 18, 2003
    #5
  6. M-a-S

    M-a-S Guest

    That's what I asked. Thanks!
    M-a-S

    "Michael Peuser" <> wrote in message news:bkd0rb$qb2$01$-online.com...
    >
    > "M-a-S" <>
    >
    > > 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
    M-a-S, Sep 18, 2003
    #6
  7. M-a-S

    Hung Jung Lu Guest

    "M-a-S" <> wrote in message news:<Vwfab.2454$>...
    > 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
    Hung Jung Lu, Sep 19, 2003
    #7
  8. M-a-S

    M-a-S Guest

    Thank you!

    "Hung Jung Lu" <> wrote in message news:...
    > "M-a-S" <> wrote in message news:<Vwfab.2454$>...
    > > 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
    M-a-S, Sep 19, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,933
    Roedy Green
    Sep 9, 2005
  2. Bo Peng
    Replies:
    4
    Views:
    273
    Bo Peng
    Mar 20, 2005
  3. Maric Michaud
    Replies:
    0
    Views:
    7,188
    Maric Michaud
    Jun 24, 2006
  4. blufox
    Replies:
    2
    Views:
    554
  5. AzamSharp
    Replies:
    2
    Views:
    178
Loading...

Share This Page