anything like C++ references?

Oren Tirosh oren-py-l at hishome.net
Sun Jul 13 09:38:01 EDT 2003


On Sun, Jul 13, 2003 at 10:58:13PM +1200, David McNab wrote:
> On Sat, 12 Jul 2003 13:53:35 -0700, Tom Plunket paused, took a deep
> breath, then came out with:
> 
> > I want to do something along the lines of the following C++ code:
> > 
> > void change(int& i)
> > {
> >    i++;
> > }
> 
> > Is there any way to do references like this in Python?
> <snip>
> 
> In Python, basic types like strings and numbers are a weird exception to
> the 'everything is an object' rule.
> 
> When you pass any other object in a function, the function gets a ref to
> that object.
> 
> But when you pass a string or numeric object, the whole thing (not a ref)
> gets passed.

Numbers and strings are objects just like everything else in Python and
are passed by reference. The only difference is that they are *immutable* 
objects so their value cannot be changed through that reference in a way
that will be visible to others who reference the same object. Under most 
circumstances a reference to an immutable object is nearly indistingishable 
from a value. But the id() builtin function or the 'is' operator quickly 
reveal the difference. 

There is no weird exception to any rule here. Some objects are mutable, 
some are not. That's all.

   Oren






More information about the Python-list mailing list