Python Forum
Pass by object reference when does it behave like pass by value or reference?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass by object reference when does it behave like pass by value or reference?
#1
I've read a lot and sort of get it, though it hasn't clicked yet.. could you explain pass by object reference like this:
In what situations does Python's pass by object reference behave like pass by value vs pass by reference? In what situations is it different?

Thanks!
Reply
#2
The Python interpreter only comprehends pass by object reference. Whether a new object is returned (kind of, but not really, like pass by value) or an existing object is modified (kind of, but not really, like pass by reference), is entirely dependent on the behavior of the object itself. The Python interpreter follows the same process behind the scenes.

Let's look at a couple of examples. First, let's explore the behavior of a+=1 where a is an immutable object, say something that looks like an integer. Here is the definition of our immutable class.

class immutable:
    def __init__(self, x):
        self.value = x

    def __iadd__(self, other):
        return immutable(self.value + other)

    def __repr__(self):
        return f"immutable({self.value})"
And here is an example of its behavior.

>>> a = immutable(1)
>>> b = a
>>> a; b
immutable(1)
immutable(1)
>>> id(a); id(b)
140551985127920
140551985127920
>>> a += 1
>>> a; b
immutable(2)
immutable(1)
>>> id(a); id(b)
140551985326256
140551985127920
>>> 
The statement a+=1 becomes the function call immutable.__iadd__(a, 1). Then a.value + 1 is used to create a new immutable instance. The new object is returned to the Python interpreter and a is changed to refer to the new object. (Note that id(a) and id(b) refer to different objects.)

Here is the definition of our mutable class.

class mutable:
    def __init__(self, x):
        self.value = x

    def __iadd__(self, other):
        self.value += other
        return self

    def __repr__(self):
        return f"immutable({self.value})"
And here is the same example but using the mutable class.

>>> a = mutable(1)
>>> b = a
>>> a; b
immutable(1)
immutable(1)
>>> id(a); id(b)
140551985424320
140551985424320
>>> a+=1
>>> a; b
immutable(2)
immutable(2)
>>> id(a); id(b)
140551985424320
140551985424320
>>> 
Just like the previous immutable example, the statement a+=1 becomes the function call immutable.__iadd__(a, 1). Instead of creating a new object, the value attribute of a is incremented. Now the underlying object has been mutated. The mutated object is returned to the Python interpreter and a is changed to refer to the mutated object. (Note that id(a) and id(b) continue to refer to the same object.)

I've skipped over some of the details. If you want more details, let me know.

casevh
Reply
#3
I suggest to read and/or watch Ned Batchelder Pycon 2015 presentation Python Names and Values


Quote:Call by value or call by reference? People also get tangled up trying to explain whether Python uses call by value, or call by reference. The answer is: neither. This is a false dichotomy, again because people are trying to mis-apply concepts from one language onto all languages.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass variables from one class to another hobbyist 18 1,209 Oct-01-2021, 05:54 PM
Last Post: deanhystad
Question How to pass a method as argument in an another method? anilanvesh 6 486 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  How to pass list of values to a API request URL chetansaip99 0 589 Sep-28-2021, 07:37 AM
Last Post: chetansaip99
  Regex - Pass Flags as a function argument? muzikman 6 635 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Value by reference issue javaben 9 710 Jul-31-2021, 12:55 PM
Last Post: javaben
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 973 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 721 May-31-2021, 07:00 AM
Last Post: bowlofred
  How to reference the relative directory when creating a photoimage kenwatts275 3 945 May-18-2021, 07:22 PM
Last Post: menator01
  Filtering characters that pass through serial xbit 0 541 May-06-2021, 11:56 PM
Last Post: xbit
  How can I pass&return ndarray between python and c++? JESuh 0 590 Mar-09-2021, 08:29 AM
Last Post: JESuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020