Names and bindings (was Re: Suggestions for good programming practices?)

Aahz aahz at pythoncraft.com
Mon Jun 24 23:12:45 EDT 2002


In article <3D17C0D0.439C5269 at engcorp.com>,
Peter Hansen  <peter at engcorp.com> wrote:
>Aahz wrote:
>> In article <bko7fa.9t.ln at grey.aerojockey.localdomain>,
>> Carl Banks  <imbosol at vt.edu> wrote:
>>>
>>>* Learn how references and the object system work as soon as possible.
>> 
>> ObBrokenRecord: "bindings", not "references".
>
>I haven't had a record player for a while :-) so would you please
>expand on that?  I've adopted "references" without giving it any
>thought.  Why is that not as good as "bindings"?

Because unless you're using the C API, you can never access the
references directly.  Python always hides the references from you:

>>> a = []
>>> b = a
>>> a.append(1)
>>> b
[1]

It's pretty clear that the assignment creates a reference to an object,
rather than having a variable contain the value.  It should be equally
clear that the dereferencing is completely automatic -- so automatic, in
fact, that you *can't* get at the reference.

Saying "binding" instead of "reference" helps to remember the critical
semantic differences in Python's object model.  It also helps when one
is trying to explain things like this:

def f(a, x=[]):
    x.append(a)
    print x

f(1)
f('foo')
L=[5]
f(9, L)
f(23)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list