Re: Pass by reference or by value?



Robert Dailey wrote:
[but he top-posted, so he should consider himself smacked on the wrist]
On 8/16/07, *Steve Holden* <steve@xxxxxxxxxxxxx <mailto:steve@xxxxxxxxxxxxx>> wrote:

Robert Dailey wrote:
> So immutable objects cannot be modified directly? I guess this means
> integers are immutable and the act of assigning to one is a
completely
> new definition?

Correct. A new value is bound to the name or item on the left-hand side
- remember, all variables are pointers to values, there's no way to
replace the value of a variable because of the automatic dereferencing.
And yes, immutable objects can't be modified. At all, period. Hence the
name.

> So if I were to create a class called Integer and
give
> it a .set() method, this would allow me to create mutable
integers, and
> thus passing in an object of type class Integer would allow me to
modify
> the value from inside the function?
>
Well, the .set() method wouldn't be called automatically on an
assignment statement but yes, if you passed an Integer object into your
function and called its .set() method from inside then it should work.

As long as you get your mutable integer implementation correct, of
course ;-). I'd suggest delegating everything except the .set() method
to the underlying integer value.

> Thanks Steve for your explanation. It was very helpful. I think I
> understand it now. By the way, by the .set method I meant:
>
> class Integer:
> def __init__( self, number=0 ):
> self._int = number
>
> def set( self, number ):
> self._int = number
>
>
> # later on....
>
> mutableInt = Integer( 5 )
> def change_me( var ):
> var.set( 6 )
>
>
> Of course, I'd probably use overloaded operators in a more realized
> example.
>
>
Fair enough, but I was trying to point out that assignment isn't an operator so you won't be able to achieve what you want by assignment to a name local to the function.

You could, of course, assign to a name local to the instance you passed in, but then there wouldn't be much point in using anything but a simple subclass of object (normally referred to as Bunch).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

.



Relevant Pages

  • Re: Addressing the last element of a list
    ... >>> Only if you have to overcome a conviction that variables behave in a ... > all objects mutable, or disallow assignment to immutable objects, ... Don't make the distinction between mutable and immutable types but ... between mutable and immutable objects. ...
    (comp.lang.python)
  • User-defined augmented assignment
    ... The problem I find with augmented assignment is it's too complex, ... the following behavior is pretty confusing: ... I use it a lot with immutable objects ...
    (comp.lang.python)
  • Re: list in a tuple
    ... Shouldn't the tuple assignment raise the exception BEFORE calling ... If you look at the bytecode generated, ... mutable and immutable objects: ...
    (comp.lang.python)