unexpected behavior: did i create a pointer?

Steve Holden steve at holdenweb.com
Fri Sep 7 15:13:05 EDT 2007


Steven D'Aprano wrote:
> On Fri, 07 Sep 2007 11:46:38 +0200, Wildemar Wildenburger wrote:
> 
>> gu wrote:
>>> hi to all!
>>> after two days debugging my code, i've come to the point that the
>>> problem was caused by an unexpected behaviour of python. or by lack of
>>> some information about the program, of course! i've stripped down the
>>> code to reproduce the problem:
>>>
>>> [snip FAQ]
>> Yes, basically you *created* a pointer. That's all that python has:
>> pointers.
> 
> No, you are confusing the underlying C implementation with Python. Python 
> doesn't have any pointers. CPython is implemented with pointers. PyPy, 
> being written entirely in Python, is implemented with Python objects like 
> lists and dicts. Jython, being implemented in Java, probably isn't 
> implemented with pointers either -- although of course the underlying 
> Java compiler might be. IronPython and Python for .Net, I have no idea 
> how they work. Could be magic for all I know. (Probably necromancy.) 
> 
> Naturally, regardless of whether you are using CPython, IronPython, PyPy 
> or some other variety of Python, the objects available to you include 
> ints, floats, strings, lists, dicts, sets and classes... but not pointers.
> 
> Nor does it include "peek" and "poke" commands for reading and writing 
> into random memory locations. Python is not C, and it is not Basic, nor 
> is it Forth or Lisp or assembler, and you shouldn't hammer the round peg 
> of Python objects into the square hole of C pointers.
> 
This seems to be obscuring the real issue for the sake of hammering an 
error in vocabulary. gu said """Yes, basically you *created* a pointer. 
That's all that python has: pointers.""" and you took issue with that, 
ultimately saying """the objects available to you include ints, floats, 
strings, lists, dicts, sets and classes... but not pointers""".

You are both right, and you are also both wrong.

Python does indeed provide a rich set of strongly typed object classes, 
and so clearly to say "Python only has pointers" is strictly an error. 
However, this overlooks the fact that *all* names in Python, and *all* 
items in container objects (lists, tuples, dicts ...) hold references to 
objects.

I mention this because it *is* significant to the semantics of 
assignment. From the point of view of a C programmer Python might 
actually look quite like a language in which all data objects had to be 
malloc'd, and the only variables were pointers to. Assignment (binding) 
in Python creates copies of references to objects, not copies of the 
objects themselves.

I have deliberately omitted discussion of the automatic dereferencing 
that takes place in Python, thereby allowing us to treat names as though 
they contained objects, but the fact remains that names *don't* contain 
objects, they contain references to objects. If you want to regard a 
reference and a pointer as two different things then I guess that's your 
nit to pick. But I don't think the original assertions quite justified 
your scathing remarks about "peek" and "poke".

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list