[FRPythoneers] propagate module variable changes

erick_bodine at comcast.net erick_bodine at comcast.net
Mon Nov 12 17:33:47 MST 2007


@property will work fine, thank you.


--ERick

 -------------- Original message ----------------------
From: "Steven Bethard" <steven.bethard at gmail.com>
> On Nov 12, 2007 4:35 PM,  <erick_bodine at comcast.net> wrote:
> > I am dynamically importing a module like the following, made simple for 
> illustrative purposes.
> >
> > module1
> > =======
> > v1a = 'one'
> > v1b = 'two'
> > v2 = 'values: %s, %s' % (v1a, v1b)
> >
> > If I import "module1" and change the value of v1a, I would like that change to 
> propagate to the module.v2 variable.
> 
> Simple answer: v2 should really be a function, not an attribute.
> 
> Complicated answer: if v2 really needs to be an attribute, you can put
> the following in module1::
> 
>     import sys
> 
>     class MyModule(type(sys)):
>         v1a = 'one'
>         v1b = 'two'
>         @property
>         def v2(self):
>             return 'values: %s, %s' % (self.v1a, self.v1b)
> 
>     sys.modules[__name__] = MyModule(__name__)
> 
> Basically, you just replace the module with a class that has the
> property object you need.
> 
> Steve
> -- 
> I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
> tiny blip on the distant coast of sanity.
>         --- Bucky Katt, Get Fuzzy




More information about the FRPythoneers mailing list