Class or Dictionary?

Dave Angel davea at ieee.org
Mon Feb 14 09:30:17 CET 2011


On 01/-10/-28163 02:59 PM, Martin De Kauwe wrote:
> On Feb 14, 12:02 pm, Terry Reedy<tjre... at udel.edu>  wrote:
>>
>>  <snip>
>
> 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 =3.4
>>>> print cons.kg_as_g
>
> is fine.
>
> But if I call import cons in another module it would be back to 1000.
> right? Whereas if I do it as a class I can pass the adjusted instance?
> E.g.
>
> from constants import Constants
> c =onstants()
>
> I can then change "c" and pass this around?
>
> Apologies if I have got that totally wrong!
>

Yep, you've got it totally wrong.  As long as your import is the kind 
you show here, changes are persistent.  Other modules that import cons 
will get the one you modified, not re-execute the source.

If you had tried something like  from cons import kg
then you'd have a separate local binding.   So don't do that.

DaveA



More information about the Python-list mailing list