reference or pointer to some object?

Jeff Shannon jeff at ccvcorp.com
Wed Jan 12 18:36:02 EST 2005


Torsten Mohr wrote:

> I still wonder why a concept like "references" was not
> implemented in Python.  I think it is (even if small)
> an overhead to wrap an object in a list or a dictionary.

Because Python uses a fundamentally different concept for variable 
names than C/C++/Java (and most other static languages).  In those 
languages, variables can be passed by value or by reference; neither 
term really applies in Python.  (Or, if you prefer, Python always 
passes by value, but those values *are* references.)  Python doesn't 
have lvalues that contain rvalues; Python has names that are bound to 
objects.  Passing a parameter just binds a new name (in the called 
function's namespace) to the same object.

It's also rather less necessary to use references in Python than it is 
in C et. al.  The most essential use of references is to be able to 
get multiple values out of a function that can only return a single 
value.  Where a C/C++ function would use the return value to indicate 
error status and reference (or pointer) parameters to communicate 
data, a Python program will return multiple values (made quick & easy 
by lightweight tuples and tuple unpacking) and use exceptions to 
indicate error status.  Changing the value of a parameter is a 
side-effect that complicates reading and debugging code, so Python 
provides (and encourages) more straightforward ways of doing things.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list