How to initialize a class variable once

Matimus mccredie at
Tue Dec 9 05:36:17 CET 2008

On Dec 8, 8:08 pm, Roy Smith <r... at> wrote:
> I've got a class with a class variable:
> class Foo:
>    _map = {}
> How do I make sure this only gets initialized the *first* time the
> module containing the class is imported?  What appears to be happening
> as it stands is each time the module gets imported, Foo._map get re-
> initialized.

Unless you are calling reload() on the module, it will only ever get
_loaded_ once. Each additional import will just yield the existing
module. Perhaps if you post an example of the behavior that leads you
to believe that the class variables are getting reinitialized I can
provide more useful help.


More information about the Python-list mailing list