getting as global as __builtin__

Larry Bates lbates at swamisoft.com
Tue Apr 13 10:19:57 EDT 2004


Create a class to hold all your variables as attributes
(which probably means changing common.py to a class) and
pass this class to each of your sub-modules as an argument.

Something like:

class common:
    def __init__(self):
        #
        # Insert anything that gets created at runtime
        # here.  These would be constants, lists, dictionaries,
        # or other data items that will be common to all your
        # functions.
        #
        self.rtvariable1="abc"
        self.rtvariable2=[]
        return

subfunction1(COMMON, arg1):
    #
    # These might be imported if you like
    #
    COMMON.rtvariable2.extend([2,3,4])
    .
    .
    .
    return


subfunction2(COMMON, arg1, arg2):
    COMMON.rtvariable1="xyz"
    .
    .
    .
    return


#
# Main program
#
from common import common
COMMON=common()
COMMON.rtvariable2.append(1)
.
.
.
#
# Call subfunction1
#
subfunction1(COMMON, arg1, arg2)
#
# Call subfunction2
subfunction2(COMMON, arg1)

I think you get the picture.

Larry Bates
Syscon, Inc.


"Mitja" <tezt at email.si> wrote in message news:c5euuf$sg$1 at planja.arnes.si...
> How to make a variable visible from __main__ as well as all other imported
> modules? I.e., how to make it share the scope with __builtin__? Saying
> __builtin__.foo='bar' works, but seems messy - foo is far from a built-in
> thingy.
>
> Background:
> ---------------------
> I'm writing a web application with a central module and a few peripheral
> ones, which provide the content of the web page to the main module. The
only
> script ever called is main.py, which then makes use of others via import.
>
> All of these modules have to have access to some variables (e.g. HTTP GET
> parameters), which are all gathered in a module common.py. Therefore, all
> modules "import common".
>
> Thing is, some of these variables are a result of database queries, so the
> can take some time to get. I would like to have them computed only once
(at
> a specific function call) and then keep them somewhere where they are
> visible to all the modules throughout the execution of the program. This
> way, we come to the problem from the first paragraph
> -------------------
>
> Are there better ways of doing it than __builtin__? Or is the entire
> approach wrong? Thanks for answers/suggestions,
>
> Mitja
>
>





More information about the Python-list mailing list