Modifying Class Object

Alf P. Steinbach alfps at
Tue Feb 9 10:08:15 CET 2010

* Steven D'Aprano:
> On Tue, 09 Feb 2010 08:19:56 +0100, Alf P. Steinbach wrote:
>> You don't have anything /but/ pointers in Python.
> x = 5

The above assigns to x a pointer to an object whose value is 5:

    x ------>  5

> y = "hello"

Ditto result:

    y ------>  "hello"

And if now do

    x = y

then the code is assigning (copying) to x the pointer in y:

    x -----------
    y ---------- > "hello"

which you can check by

    assert( x is y )


    assert( id( x ) == id( y ) )

and which becomes essential to understand when that object is mutable...


   x = [1, 2, 3, 4]
   y = "hello"

   y = x
   y.append( 5 )

   print( x )

> You want us to accept that the values of x and y are some mysterious 
> pointer entities rather than the integer object 5 and the string object 
> "hello".

The basics of pointers are simple.

You can copy pointers (assignment, argument), and you can check for equality 
(is), and you can refer to the object that's pointed to (attribute access, 
indexing, operators). And in CPython you can even obtain the pointer value as an 
integer via id().

So there's nothing mysterious.

In particular

    x = [1, 2, 3, 4]
    y = x

does not copy the object value, it only copies the pointer.

That also matters for e.g.

    x = 10000000*"A"
    y = x

And aliasing, like in the last three examples above, is pretty difficult to 
understand without a pointer view of things.

And if aliasing is not understood, then forever stranded at beginner's level.

> At the point that your explanation depends on the reader understanding 
> that the value of a variable x is not actually the thing that they assign 
> to x, but some mysterious, invisible entity that exists entirely inside 
> the implementation where the user can never reach, you probably should 
> rethink your attachment to that explanation. No wonder so many Java 
> programmers are still confused by it.

Thanks for thinking about my writings.

As it happens I've already written about pointers in chapter 2, although not 
mentioning the word "pointer". :-)

I made do with the term "reference", because in Python there's not anything else 
(in particular there's no pass by reference) that could cause confusion.

However, that cop-out (is that the right term?) wouldn't work for text based on 
a language like C++, which has another kind of reference, or for that matter C# 
which is like Python and Java except that C# also has pass by reference...

Some mixed feedbacks on that writing, but it's section 2.6.7 "References & 
automatic garbage collection" in ch 2 "Basic concepts" at <url:> (I hope I typed the URL correctly).

> It might help your case if we had a word for the thing that is actually 
> assigned to the variable. In plain English that is "value", but you want 
> to use that for the pointer to the thing-that-we-otherwise-would-call-
> value, which leaves us no simple way to talk about the int 5 and string 
> "hello".

There are two values: the pointer value, and the object value.

In CPython:

   >>> a = "blah"
   >>> str.format( "Pointer = {0}, object = {1}".format( id( a ), a ) )
   'Pointer = 12090272, object = blah'
   >>> _

More simply, there's the pointer and there's the object pointed to.

Or, in the not-very-general-but-I-think-works-in-Python terminology that I used 
in 2.6.7, there's the reference and there's the object.

So the thing that's actually assigned to a variable is in language-independent 
terminology a "pointer" (or pointer value), although that doesn't mean C or 
Pascal or whatever specific language pointer but just a copyable object 
reference, and perhaps in Python-specific terminology it's just a "reference".

I don't know better terms...


- Alf

More information about the Python-list mailing list