Simulating multi-dim array problem - OR - reference confusions

Steve Holden sholden at holdenweb.com
Wed Apr 17 09:12:57 EDT 2002


"holger krekel" <pyth at devel.trillke.net> wrote ...
[In reply to "Shane Hoversten" <srh232 at nyu.edu> ] ...
> > I'm doing code that needs the functionality of multi-dimensional
> > arrays. I'm getting around this right now by using lists of lists, which
> >  seems to work right.  For instance:
> >
> > >>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
> > ...
> > b = a[0]
> >
> > That b is actually a reference to the appropriate list in a, which
> > is just what I need.
>
> actually speaking in python semantics
>
> b=a[0]
>
> means that the "name 'b' is bound to 'a[0]'". Internally
> the reference count of the object a[0] is incremented.
>
Well, newbies might avoid mis-perceptions by reading the Python as "name 'b'
is bound to the object currently bound to 'a[0]'", to avoid any belief that
anything in a changes as a result of the assignment. In strict terms, a[0]
doesn't *have* a reference count, as far as I can tell without reading the
interpreter code, though both a and the object bound to a[0] will have a
reference count.

> the difference is that after you change b with e.g.
>
>   b = ['bert']
>
> a[0] will not have changed. You just changed the binding
> of the name 'b' again -- not touching 'a' in any way
> (except that the reference count of a[0] is decremented)!
>
Again, it's important to realise that *nothing* in 'a' changes as a result
of this rebinding, though the reference count of the object referenced by
a[0] will have been reduced by one.

> > Here's are my questions:
> >
> > 1) Am I going to get into trouble assuming (using the example above)
> > that b is basically a reference to the sublist a[0]?  I'm not clear how
> > sharing works in python.
[ ... ]
Well, since that's exactly what b *is*, you'll only get into the trouble you
make for yourself. You seem to have a pretty clear understanding of Python's
binding model.
>
> > 2) Is there a way I can print out the address of a "reference"?  In
> > particular I want some concise symbolic representation - like a memory
> > address, or a handle id, or SOMETHING - and not a verbatim
> > reconstruction of the list structure which is how python's "print"
> > function does it.  If I have elements containing references to elements
> > containing references to elements it's impossible to follow what's going
> > on with the chain.  I'd like to have it print something like:
> >
> > [[x, y, z, 0x123123], [a, b, c, 0x123958], [q, w, e, 0x358481]]
>
[ ... ]
The id() finction gives a unique value for each distinct object in Python.
Rumour has it that in the current C implementation of Python the value id()
returns is not unrelated to the object's address in memory, although I don't
believe this is guaranteed by the documentation.

I understand that the "is" test actually tests for equality of id() values
(since two references to the same object must give the same id() value).

However, you may have to get involved in type-checking to determine whether
to print out the value of a reference or its id() value. This may oir may
not ultimately be a good thing.

Finally, I'm not sure how you would get Python to print a list of lists in
the form you originally mentioned, since it would normally dereference the
names a, b, c, etc., and simply print the values they referenced. You might
therefore get either

[[1, 2, 3, 0x123123], [7, 8, 9, 0x123958], [12, 13, 14, 0x358481]]

or possibly

[['x', 'y', 'z', 0x123123], ['a', 'b', 'c', 0x123958], ['q', 'w', 'e',
0x358481]]

but not, without some programming effort on your part,

[[x, y, z, 0x123123], [a, b, c, 0x123958], [q, w, e, 0x358481]]

regards
 Steve








More information about the Python-list mailing list