imported var not being updated
gherron at islandtraining.com
Tue Mar 9 17:34:38 CET 2010
John Posner wrote:
> On 3/8/2010 11:55 PM, Gary Herron wrote:
>> The form of import you are using
>> from helpers import mostRecent
>> makes a *new* binding to the value in the module that's doing the
>> What you can do, is not make a separate binding, but reach into the
>> helpers module to get the value there. Like this:
>> import helpers
>> print helpers.mostRecent
> Gary, are you asserting that in these separate situations:
> from helpers import mostRecent
> x = mostRecent
> import helpers
> x = helpers.mostRecent
> ... the name "x" will be bound to different objects?
No they are bound to the same object ... to start with. But if, as the
OP indicated, something in the helpers module changes that module's
binding of 'mostRecent', then the only helpers.mostRecent in your two.py
will notice that change. The variable 'x' in both samples and the
'mostRecent' value in one.py will remain unchanged.
That's true for mutable *and* immutable values -- however, mutable
values admit a further consideration: Since all 'x's and 'mostRecent's
refer to the same value, any mutation is noticed in all cases. But,
*any* assignment ( with = or of the form *from helpers import
mostRecent* ) breaks the reference to the shared value.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list