[Python-bugs-list] [ python-Bugs-500539 ] Inaccuracy(?) in tutorial section 9.2

noreply@sourceforge.net noreply@sourceforge.net
Tue, 08 Jan 2002 12:27:37 -0800


Bugs item #500539, was opened at 2002-01-07 12:15
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=500539&group_id=5470

Category: Documentation
Group: Not a Bug
Status: Open
Resolution: Works For Me
Priority: 5
Submitted By: Andrew Koenig (arkoenig)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Inaccuracy(?) in tutorial section 9.2

Initial Comment:
Section 9.2 paragraph 3 defines a namespace as a
mapping from names to objects.  Perhaps I'm being picky
here, but isn't a namespace a mapping from names to
object references?  For example, doesn't executing the
expression "modname.the_answer = 42" as shown in
paragraph 5 cause modname.the_answer to refer to a
different object than the one to which it referred
previously, rather than affecting the specific object
to which modname.the_answer refers?

----------------------------------------------------------------------

>Comment By: Andrew Koenig (arkoenig)
Date: 2002-01-08 12:27

Message:
Logged In: YES 
user_id=418174

Aha! -- Now I understand.  You're thinking of a namespace 
as a collection of named references to objects that can be 
searched associatively by name :-)  Moreover, the name-
reference relation is bijective.  That is, if I write

a = 3
b = 4

then there is a unique entity -- which I have been calling 
a reference -- associated with `a' and another such entity 
associated with `b'.  Moreover, because the relation is 
bijective, the entity with which `a' is associated is not 
associated with any other name, and similarly for `b'.  
Because of this bijectivity, there is never any reason to 
think about this entity separately from its name, and 
therefore you do not need a separate term for it.

Incidentally, C++ uses both the term `lvalue' and 
`reference,' with `lvalue' denoting a syntactic context and 
`reference' denoting a particular property of types.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-01-08 12:10

Message:
Logged In: YES 
user_id=6380

I read everything. :-) Let me butt in here. (I also probably
wrote that section in the tutorial. :-)

I know Algol-68 and am familiar with its use of the term
"reference". Fred probably isn't and thinks of references as
equivalent to pointers; but I'm sure he's familiar with the
C term "l-value" which is C's name for what Algol-68 cals a
reference. (I don't know what C++ calls it these days; isn't
reference a specific thing involving an & operator there?)

Python just doesn't use the concept of "object reference" in
the same way as Algol-68 uses it. I mean it when I say that
a namespace maps names to objects! The difference is that I
consider assignment a change to the mapping. It can't be a
change to a "reference" (in the Algol-68 sense) since the
reference may not yet exist (if the name wasn't bound
before).

Also, Python *definitely* doesn't have the concept of a
reference to a reference (C's ** types, Algol-68's ref ref).
While there most definitely is a piece of memory holding an
object pointer in the *implementation* of Python's
namespaces, and in other collections of objects like lists,
there's no way in Python to talk about that piece of memory
in a unified way. You'd have to say "the name X in mapping
Y" or "the index K in sequence L" or something like that.

I hope this helps everyone's understanding!

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2002-01-08 08:49

Message:
Logged In: YES 
user_id=3066

Re-opened for further consideration when I have more time.

----------------------------------------------------------------------

Comment By: Andrew Koenig (arkoenig)
Date: 2002-01-08 08:22

Message:
Logged In: YES 
user_id=418174

Actually, I was saying the opposite.  Executing "x = 42"
certainly does not mutate the object that was previously
associated with x, but rather causes x to be associated with
a new object, namely 42.  To me that suggests that 42 is an
object and x is something else, perhaps a way of accessing
an object.

You are right that this kind of confusion is common among
people who write about C++, but you are not right that I
used C++ as the basis for my original comment.  I first
became aware of the importance of distinguishing between
objects and object references in Algol 68, many years before
C++ ever existed, and there is a wonderful piece in Lewis
Carroll's ``Through the Looking Glass'' about this very
issue.

More seriously, if there is no program I can ever write that
is capable of distinguishing between an object and an object
reference, then it doesn't matter.  However, I think I can
write such a program:

a = [42]; b = [a, a]

Now it doesn't make sense to say that b[0] and b[1] are the
same object, because they aren't.  Proof:  If I execute
b[0]=79, b[1] is still [42].  On the other hand, b[0] and
b[1] do refer to the same object, and it is that distinction
that I am suggesting that it is important to draw.

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2002-01-08 07:20

Message:
Logged In: YES 
user_id=3066

There is no difference between an object reference and an
object in Python code, so the statement in the tutorial is
correct.  The behavior you are describing as "object"
behavior seems to assume that there's some sort of
assignment  which mutates the object which is the current
value of the assignment target, but there is no such
operator.  I can see where this might be confusing for
programmers coming from C++, though.  ;-)

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=500539&group_id=5470