Global package variable, is it possible?

Chris Allen ca.allen at gmail.com
Mon Aug 6 18:16:15 EDT 2007


On Aug 6, 12:41 am, Bruno Desthuilliers <bruno.
42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> Chris Allen a écrit :
>
>
>
> > Hello fellow pythoneers.  I'm stumped on something, and I was hoping
> > maybe someone in here would have an elegant solution to my problem.
> > This is the first time I've played around with packages, so I'm
> > probably misunderstanding something here...
>
> > Here's what I'd like to do in my package.  I want my package to use a
> > configuration file, and what I'd like is for the config file to appear
> > magically in each module so I can just grab values from it without
> > having to load and parse the config file in each package module.  Not
> > quite understanding how the __init__.py file works, I expected it to
> > be as easy as just setting up the ConfigParser object and then I
> > figured (since a package is a module) that it would now be global to
> > my package and I could access it in my modules, but I was wrong...  I
> > don't want to have to pass it in to each module upon init, or anything
> > lame like that.  A main reason for this is that I'd like to be able to
> > reload the config file dynamically and have all my modules
> > automatically receive the new config.  There must be a way to do this,
> > but seeing how __init__.py's namespace is not available within the
> > package modules, I don't see a simple and elegant way to do this.
> > Does anybody have any suggestions?  Thanks!
>
> Hi Chris...
> I've read all the thread, and it seems that your problem is mostly to
> share a single dynamic state (the config) between several modules. So I
> do wonder: have you considered the use of the Singleton pattern (or one
> of it's variants...) ?

Thanks, I don't know anything about python singletons.  But I'll look
it up.




More information about the Python-list mailing list