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/1236318.html





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

Reply via email to