Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imported var not being updated

0 views
Skip to first unread message

Alex Hall

unread,
Mar 8, 2010, 11:22:27 PM3/8/10
to pytho...@python.org
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!!

--
Have a great day,
Alex (msg sent from GMail website)
meh...@gmail.com; http://www.facebook.com/mehgcap

Gary Herron

unread,
Mar 8, 2010, 11:55:01 PM3/8/10
to pytho...@python.org

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


Alex Hall

unread,
Mar 8, 2010, 11:59:00 PM3/8/10
to Gary Herron, pytho...@python.org
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.

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

John Posner

unread,
Mar 9, 2010, 9:18:42 AM3/9/10
to Gary Herron, meh...@gmail.com
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

Bruno Desthuilliers

unread,
Mar 9, 2010, 9:48:59 AM3/9/10
to
John Posner a �crit :

Nope. What he's saying is that one.x and two.x are two different names -
each living in it's own namespace - that happen to be bound to the same
object. Now rebiding one.x to a different object will _of course_ have
no impact on the object two.x is bound to.

That's exactly the same as:

one = dict()
two = dict()

# one['x'] and two['x'] refer to the same object
one['x'] = two['x'] = ["foo", "bar"]
print one['x'], two['x'], one['x'] is two['x']

# mutating one['x'], visible in two['x']
# (of course since it's the same object)
one['x'].append("baaz")
print one['x'], two['x'], one['x'] is two['x']

# now rebind one['x']
one['x'] = 42

# obvious result: one['x'] and two['x'] now refer to 2 different objects
print one['x'], two['x'], one['x'] is two['x']


If in doubt about namespaces, think dicts. Namespaces are like dicts -
and are often nothing else that a plain old dict FWIW.

HTH

John Posner

unread,
Mar 9, 2010, 11:49:37 AM3/9/10
to

Sure -- my bad, Python 101. In this case, the module *helpers* is the
equivalent of a container object, one of whose attributes *mostRecent*
gets bound to a series of immutable string objects.

>
> That's exactly the same as:
>
> one = dict()
> two = dict()
>
> # one['x'] and two['x'] refer to the same object
> one['x'] = two['x'] = ["foo", "bar"]
> print one['x'], two['x'], one['x'] is two['x']
>
> # mutating one['x'], visible in two['x']
> # (of course since it's the same object)
> one['x'].append("baaz")
> print one['x'], two['x'], one['x'] is two['x']
>
> # now rebind one['x']
> one['x'] = 42
>
> # obvious result: one['x'] and two['x'] now refer to 2 different objects
> print one['x'], two['x'], one['x'] is two['x']
>
>
> If in doubt about namespaces, think dicts. Namespaces are like dicts -
> and are often nothing else that a plain old dict FWIW.

No doubts here, I hope. I even tend to see namespaces where, strictly
speaking, they don't exist. As I said at the end of a recent (and
flame-ridden) thread [1]:

-----------
* A dict is a collection of user-devised names, each of which
is assigned to an object.
* A list/tuple is an interpreter-maintained collection of integer
names (0, 1, 2, ...), each of which is assigned to an object.
-----------

So in my world, in mylist[cnt+1], the expression *cnt+1* is equivalent
to the integer "name" 4 (if cnt==3, that is), so that

mylist[cnt+1] = obj.subobj.attr

... is just a NAME2 = NAME1 kind of assignment statement, binding an
additional name to an object that already has a name.

Tx,
John

[1] http://mail.python.org/pipermail/python-list/2010-February/1236318.html

0 new messages