imported var not being updated

Gary Herron 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:
> <snip>
>>
>> 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
>> import.
>
> <snip>
>
>> 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:
>
> one.py:
>
>   from helpers import mostRecent
>   x = mostRecent
>
> two.py:
>
>   import helpers
>   x = helpers.mostRecent
>
>
> ... the name "x" will be bound to different objects?
>
> Tx,
> John

Hi John,


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.

Gary Herron


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100309/2aefd381/attachment.html>


More information about the Python-list mailing list