Finding the instance reference of an object

Mel mwilson at the-wire.com
Tue Nov 4 17:02:35 CET 2008


Craig Allen wrote:
> That is, python lets you change object references pointing to
> immutibles, which looks like changing the value referenced,  by
> rebinding.
> 
> So is that objectionable?

OK once in a while, but it wouldn't do for everyday.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
# Case A:
>>> a=2
>>> b=3
>>> a+b
5
# Case B:
>>> id(a)
135720748
>>> id(b)
135720736
>>> id(a+b)
135720712
>>> id(a)+id(b)
271441484

In talking about Python programs, we'd really like to talk like case A. 
Meaningful values are tagged with names, passed around, operated on.  We
get our result and leave.  Case B adds a level of indirection that, if we
mention it, just obscures our actual task of summing two numbers.

Consider the fragment

def f (x, y):
    return x+y
a=2
b=3
c = f(a, b)

We can imagine a cross-namespace assignment operator (which we could write
=\= if we needed more weird glyphs.)  It works exactly like ordinary
assignment except that the LHS is evaluated in a different namespace from
the RHS.  We could use our new operator to trace the execution of this code
fragment.  The core of it would be

x =\= a         # outer to inner assignment
y =\= b
c =/= x+y       # inner to outer assignment

It's ordinary assignment all the way, except for being from an outer to an
inner, or from an inner to an outer namespace.  Note how changing to
mutable arguments makes no difference (literal arguments this time):

c = f ([2], [3])

x =\= [2]
y =\= [3]
c =/= x+y

Replacing =\= and =/= with = loses us our cross-namespace conceit, but the
code still runs and does what Python code does.

So why import the wrong terminology from some other field, then try to save
it by mangling the definition of 'value' so that it's no use in discussing 
what a program is supposed to do?

ob: Python, I refer you to the _Beyond the Fringe_ sketch "Portrait from
Memory", a reminiscence by a pseudo Bertrand Russell ("Moore, have you some
apples in that basket?".)  You want to have to talk like this all the time? 
See what I mean?




More information about the Python-list mailing list