[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: imported var not being updated

Gary Herron

3/9/2010 4:55:00 AM

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


3 Answers

John Posner

3/9/2010 2:19:00 PM

0

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

3/9/2010 2:49:00 PM

0

John Posner a écrit :
> 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?

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

3/9/2010 4:50:00 PM

0

On 3/9/2010 9:48 AM, Bruno Desthuilliers wrote:
> John Posner a écrit :
>> 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?
>
> 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.

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/12...