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
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
<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
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
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