Loading a file only once into an object and being able to access it from other modules - still have a problem

Peter Hansen peter at engcorp.com
Thu Dec 9 18:35:01 CET 2004

Philippe C. Martin wrote:
> class SC_ISO_7816:
> __m_loaded = None

Please don't use tabs in code you post here.  Many newsreaders
have trouble displaying them, and make the code look like it
does above (i.e. no indentation at all) which makes it hard to
> ......
>     def __init__(self):
>         """
>         """
>         if SC_ISO_7816.__m_loaded == None:
>             SC_ISO_7816.__m_loaded = True

Here you are directly accessing a "class attribute",
which all instances will see.  You knew that.

> 	    print 'LOADING'
>             self.__Load()

What does this do?  You don't show the code, but from
the other stuff I infer it is creating an attribute
named self.SW1_DICT.  Is that right?

>             print self.SW1_DICT

And here you print it.

> if I import and/or create more than one instance of SC_ISO_7816, when I see
> 'LOADING' then I also see a fully populated dictionary.
> But when I see (second or more time)
> 'DICT ALREADY LOADED', then my dict is emtpy
> What else am I not understanding  ?

Well, from the looks of things, you don't seem to understand the
basic idea of instances and instance attributes.  There is a key
difference between SC_ISO_67816.__m_loaded and self.SW1_DICT,
and that is that the former one is seen by *all instances*, while
the latter is an attribute in *only a single instance*.

Given that, you shouldn't expect multiple instances of
the class to be able to see the SW1_DICT attribute that
you set in self.__Load().


More information about the Python-list mailing list