# Re: Pointer variables possible?

Steven D. Majewski (sdm7g@elvis.med.Virginia.EDU)
Tue, 5 Apr 1994 20:11:00 GMT

In article <Cnsn6q.1pL@spk.hp.com>, Bill Baker <baker@spk.hp.com> wrote:
>I have come upon a situation that I feel would benefit from storing pointers
>to several items in a list type that would enable insertions at these points.
>I don't know much about the internal workings of python and whether this would
>be possible but with C it would only require the use of an address pointer. I
>have looked through the python references I have but being new to python I
>don't know if this is possible.
>
> Bill

You can do an insertion into a Python list with the "slice" operator:
( But I don't know if this is what you mean. )

>>> a = [ 1, 2, 3, 4 ]
>>> a
[1, 2, 3, 4]
>>> a[2:3]
[3]
>>> a[2:3] = [3,2,1,2,3]
>>> a
[1, 2, 3, 2, 1, 2, 3, 4]
>>>

Python does not have pointers, but references to mutable objects
ARE references to the OBJECT, and not the VALUE.

>>> b = [ a, 'test', a ]
>>> b
[[1, 2, 3, 2, 1, 2, 3, 4], 'test', [1, 2, 3, 2, 1, 2, 3, 4]]
>>> a[3:]
[2, 1, 2, 3, 4]
>>> del a[3:] # truncate 'a'
>>> a
[1, 2, 3]
>>> b
[[1, 2, 3], 'test', [1, 2, 3]] # b = [a, 'test', a] 's "a" is truncated
>>> a = 'xxx' # this binds the NAME "a" to a new value
>>> b # and doesn't affect b's copy of the OBJECT 'a'
[[1, 2, 3], 'test', [1, 2, 3]] # which exists as long as there are references
>>>

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics