imported var not being updated
mehgcap at gmail.com
Tue Mar 9 05:59:00 CET 2010
Thanks, it worked as expected. I guess I figured that Python would
read my mind and realize that I wanted mostRecent to act globally for
the program, imported as a copy or accessed in its own namespace
(right term?) Oh well, the day computers can read thoughts like that
is the day programmers are out of a job... Thanks again.
On 3/8/10, Gary Herron <gherron at islandtraining.com> wrote:
> Alex Hall wrote:
>> Hello all:
>> I have a project with many pyw files. One file holds a variable and a
>> function that I find myself using across many other pyw files, so I
>> called it helpers.pyw. The variable is "mostRecent", which is a string
>> and is meant to hold a string so I know what the program most recently
>> output; in all the other pyw files, every time the program outputs
>> something, I include the line
>> where output is a string and helpers has been imported.
>> The problem is that mostRecent does not seem to be updated at all, and
>> I cannot figure out why. I declare it with empty quotes initially, but
>> the program should keep updating it. Everytime I go to see what its
>> value is, though, it is still apparently empty. Here is a basic
>> import helpers
>> def something():
>> output="I am the most recently output string!"
>> #end def
>> from helpers import mostRecent
>> print(mostRecent) #prints nothing
>> Any thoughts on what I am doing wrong? Thanks!!
> 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. Rebinding the var in a different module does not affect this.
> It's similar to this:
> a = 'string'
> b = a
> a = 'another string'
> won't affect b's value.
> 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
> That will work as you hope.
> Gary Herron
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap
More information about the Python-list