How do can you make a variable static?

Darrell news at dorb.com
Thu Jun 24 20:41:24 EDT 1999


> from foo import foo
>
> SymHanDict={0:0}
>
> foo()                            #each call to foo() should add items to
> the dictionary
> foo()
> foo()
>
> *** the following contained in module foo.py ************************
> def foo():
>     global SymHanDict

Global means look for it in the foo.py module

In this case SymHanDict is in another file and your best bet is to pass it
to foo(). If you just don't want to do that try something like this in the
module where you plan to call foo().

import foo

SymHanDict={0:0}

def myFoo():
    foo.foo(SymHanDict)


Passing a parm is much cleaner than globals anyway.
Might I suggest using a class:

class Foo:
    def __init__(self, dict):
            self._myDict=dict

    def __call__(self):
            """Do foo stuff here"""
            pass

myFoo = Foo(SymHanDict)

    # Call the foo thing here.
myFoo()


--Darrell








More information about the Python-list mailing list