Class or Dictionary?

Terry Reedy tjreedy at udel.edu
Mon Feb 14 09:12:07 CET 2011


On 2/13/2011 8:33 PM, Martin De Kauwe wrote:

> Cool! Thanks this seems straight forward, however if I do it this way
> then once I change it (i.e. after reading user param file) I can't
> pass the changed version to another module can i? Or am I being
> stupid? Unless I do it my way I think
>
> import cons
>>>> print cons.kg_as_g
>>>> 1000.0
>>>> cons.kg_as_g = 23.4
>>>> print cons.kg_as_g
>
> is fine.
>
> But if I call import cons in another module it would be back to 1000.
> right?

Wrong. There is one and only one module object, imported into (which 
means bound to a name in) as many modules as you want. If you change an 
attribute, it is changed for all. If you add attributes from any module, 
they are added for all, regardless of whether or not they have already 
imported it. No different from changing a list or dict with multiple names.

The math module is read only. User python-coded modules are read-write.

-- 
Terry Jan Reedy




More information about the Python-list mailing list