[Edu-sig] Sticky-note Analogy

Christopher Thoday chris at cthoday.uklinux.net
Tue May 13 23:10:26 CEST 2008


Would it not be better to describe the differences between C and Python 
variables directly rather than using an analogy?

In C, a variable is the address of a storage location that contains its 
value. If that value is itself an address then the variable is described 
as a pointer.

In Python, a variable is a reference to an object that has a type as 
well as a value.

The statement:

    a= b = c;

results in three separate values in C but only one in Python.  The 
effect is the same in both languages provided that c refers to a 
constant. However, if c is a mutable object, such as a list, then 
changing the value of one variable changes them all. Anyone coming to 
Python from C may be confused by this as it is not clearly described in 
any of the books on Python that I have read.

When passing an argument to a function, C uses pass-by-value whereas 
Python uses what is in effect pass-by-reference. In order to obtain 
pass-by-reference in C the value must be a pointer. Inside the function 
the pointer must be dereferenced by prefixing it with an asterisk to 
obtain the value.

Eur Ing Christopher Thoday
Software Engineer



More information about the Edu-sig mailing list