Pass-by-reference : Could a C#-like approach work in Python?

Michael Chermside mcherm at mcherm.com
Fri Sep 12 07:50:20 EDT 2003


I (Michael Chermside) wrote:
> > I started by looking for use cases... where would this proposed
> > pass-by-reference actually prove useful? Stephen basically found
> > one good use case:
> > 
> >   I wish to "transfer ownership" of an object that gets passed
> >   into a method call, and protect against the caller accidently
> >   continuing to use the object. [So, we'll use a "ref" parameter and
> >   set the caller's variable to None.]

Duncan Booth replies:
> That only works if the caller hasn't assigned that object to any other 
> variables. If they have, they can still accidentally continue to use the 
> object. Since you have to trust the caller anyway, why not trust them to 
> stop using the object?

Indeed, his use case is not the strong "provides actual security against
misuse", but the MUCH weaker "protects against against one particular
(fairly common) caller error which is so simple a mistake it would likely
be caught by good unit tests anyhow". Which is part of why all agree
that it's such a weak use case.

-- Michael Chermside






More information about the Python-list mailing list