Help on object scope?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Feb 25 17:18:25 EST 2007


bmaron2 at hotmail.com writes:

> My code will have about 10 of these global objects, all of which
> interact with eachother. It seems silly to have to pass 10
> parameters around to each instance I work with.

Yes. A better solution would be to put them inside a module, and
import that module. Then the objects are available at any level as
attributes of the imported module.

===== foo.py =====
def spam():
    return max(eggs, ham)

eggs = 17
ham = 12
=====

===== bar.py =====
import foo

def main():
    spork = do_something_with(foo.eggs)
    foo.spam(spork)
=====

-- 
 \    "Know what I hate most? Rhetorical questions."  -- Henry N. Camp |
  `\                                                                   |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list