[Edu-sig] Python for Algorithms and Data Structures...

Markus Gritsch gritsch@iue.tuwien.ac.at
Fri, 23 Aug 2002 16:12:01 +0200


Markus Gritsch wrote:
> Seabrook, Richard wrote:
> 
>>  
>> I think both views have merit.  On one hand, pointers aren't essential
>> for any particular algorithm, they are a subject in and of themselves
>> requiring considerable exposure and experimentation to grasp.
> 
> 
> Agreed.
> 
>> On the
>> other, languages that do not have pointers invariably get into some
>> explanation difficulties sooner or later.  For example, in Python some 
>> copying and assignment operations result in two names referring to the
>> same object, rather than two unique objects.  In languages with pointers
>> this is much easier to explain, once students grasp the idea of a 
>> pointer.
>> Dick S.
> 
> 
> I think that the lack of pointers in Python is no shortcoming as long as 
> you explain to the students, that the assignment operator actually 
> assigns the *reference* of an object to the new object and that only in 
> the case of immutable types a new object is created.

Actually, using the pure assignment operator together with immutable 
objects *also* only copies references:
   >>> a = 5
   >>> id(a)
   135109992
   >>> b = 5
   >>> id(b)
   135109992
This is done for performance and memory reasons.  Only if you assign a 
new value to 'b' a new object is created.
   >>> b = 10
   >>> id(b)
   135109872
But that are just details which don't have to be explained to students 
for the sake of grasping the reference-stuff.

Markus