unexpected behavior: did i create a pointer?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Sep 8 05:44:24 EDT 2007


On Sat, 08 Sep 2007 10:07:14 +0200, Peter Otten wrote:

> Am Fri, 07 Sep 2007 13:10:16 +0000 schrieb Grant Edwards:
> 
>> On 2007-09-07, Peter Otten <__peter__ at web.de> wrote:
>>> Am Fri, 07 Sep 2007 10:40:47 +0000 schrieb Steven D'Aprano:
>>>
>>>> Python doesn't have any pointers.
>>>
>>> Thinking of python variables or "names" as pointers should get you a
>>> long way when trying to understand python's behaviour.
>> 
>> But thinking of them as names bound to objects will get you further
>> (and get you there faster). ;)
> 
> Understanding a new system in terms of one I already know works for me.
> When terminology and the system it describes make a perfect fit that is
> a strong indication that you have reached a dead end.

Ways that Python objects are not like C pointers:

(1) You don't have to manage memory yourself.

(2) You don't have typecasts. You can't change the type of the object you 
point to.

(3) Python makes no promises about the memory location of objects.

(4) No pointer arithmetic.

(5) No pointers to pointers, and for old-school Mac programmers, no 
handles.

(6) No dangling pointers. Ever.

(7) There's no null pointer. None is an object, just like everything else.

(8) You can't crash your computer by writing the wrong thing to the wrong 
pointer. You're unlikely even to crash your Python session.



Ways that Python objects are like pointers:

(1) ... um... 

Oh yeah, if you bind the _same_ object to two different names, _and_ the 
object is mutable (but not if it is immutable), mutating the object via 
one name will have the same effect on the object -- the same object, 
naturally -- bound to the other name.

You know, maybe because I came to Python with no C experience, I never 
had trouble with the "unexpected behaviour" that so confused the original 
poster. It's just obvious.


-- 
Steven.



More information about the Python-list mailing list