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 a mongdb command to a module and execute it. cspower 0 277 Feb-03-2024, 09:54 PM
Last Post: cspower
  How to pass encrypted pass to pyodbc script tester_V 0 807 Jul-27-2023, 12:40 AM
Last Post: tester_V
  how to return a reference to an object? Skaperen 8 1,111 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  How to pass -Xutf8 parametri to python tierox 2 822 Jun-07-2023, 07:17 AM
Last Post: tierox
  Unresolved reference problem john7 1 795 Oct-18-2022, 02:32 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,775 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Points not plotting with reference to x-tick labels Mark17 2 1,231 Jun-14-2022, 05:38 PM
Last Post: Mark17
  Pass variable to subprocess paulo79 4 9,959 Apr-12-2022, 12:35 PM
Last Post: DeaD_EyE
  reference from another function Frankduc 10 2,247 Mar-01-2022, 01:10 PM
Last Post: Frankduc
  How to pass variables from one class to another hobbyist 18 10,396 Oct-01-2021, 05:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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