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
  pass arguments from bat file to pyhon script from application absolut 2 992 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Endgame engine with non-standard figures doesn't behave correctly max248 0 466 Dec-07-2024, 12:44 PM
Last Post: max248
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,013 Nov-21-2024, 11:48 PM
Last Post: haihal
  How returns behave in a function with multiple returns? khasbay 1 812 May-19-2024, 08:48 AM
Last Post: deanhystad
  how to pass a mongdb command to a module and execute it. cspower 0 852 Feb-03-2024, 09:54 PM
Last Post: cspower
  How to pass encrypted pass to pyodbc script tester_V 0 1,723 Jul-27-2023, 12:40 AM
Last Post: tester_V
  how to return a reference to an object? Skaperen 8 4,774 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  How to pass -Xutf8 parametri to python tierox 2 1,753 Jun-07-2023, 07:17 AM
Last Post: tierox
  Unresolved reference problem john7 1 1,615 Oct-18-2022, 02:32 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 8,954 Jul-01-2022, 06:34 AM
Last Post: SharonDutton

Forum Jump:

User Panel Messages

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