Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Value by reference issue
#4
Quote: So modifying one, modifies them all

Not true. These are ints and they are fundamentally not modifiable. So none of them can ever change. This is different than something like a list.

You have demonstrated that they are the same right now. You have not demonstrated that changing one changes another one. In fact, lines 17-20 show that (attempting) to change one of the variables leaves the other variable value unmodified.


Quote: The snipped and simplified version of the original program, is shown below. Thus when it is assigned to channel, channel points to the memory location.

Not quite. That's how C would do it. In python, the variable points to an object. The object is the main thing, not the address.

Quote:Changing adcnum changes the value in memory,
.

Assigning adcnum (using "=") should not modify the object it pointed to before. If it points to a mutable object, you can change that object through some methods, but assignment doesn't do that. You can't change an int anyway.

Your second program doesn't seem to be changing adcnum at all. You assign a value to channel, then you assign a value to commandWord. I don't see the problem in this example yet.

Why should commandWord be expected to hold the original value when you assign it a modified value on line 3?
Reply


Messages In This Thread
Value by reference issue - by javaben - Jul-30-2021, 10:40 PM
RE: Value by reference issue - by bowlofred - Jul-30-2021, 11:32 PM
RE: Value by reference issue - by javaben - Jul-30-2021, 11:59 PM
RE: Value by reference issue - by javaben - Jul-31-2021, 02:36 AM
RE: Value by reference issue - by bowlofred - Jul-31-2021, 12:22 AM
RE: Value by reference issue - by deanhystad - Jul-31-2021, 12:24 AM
RE: Value by reference issue - by deanhystad - Jul-31-2021, 04:47 AM
RE: Value by reference issue - by javaben - Jul-31-2021, 04:48 AM
RE: Value by reference issue - by deanhystad - Jul-31-2021, 04:59 AM
RE: Value by reference issue - by javaben - Jul-31-2021, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,579 Sep-07-2020, 08:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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