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
helpers.mostRecent=output
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
sample:

randomFile.pyw
---
import helpers

def something():
  output="I am the most recently output string!"
  helpers.mostRecent=output
#end def

file2.pyw
---
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


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to