Python Forum
how can a variable change if I haven't changed it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can a variable change if I haven't changed it?
#2
First, list and iter are names of classes in python. You shouldn't use them as variable names or you hide the ability to use the class namespaces.

You don't have anything named oldmatrix in your code, but I can guess what you've done is assumed that assigning a list copies the data rather than just a reference to it. (Please put your code in "python" tags so that the indentation is preserved).

I've made some other code to show how this can affect you and one way to avoid it.

baselist = [0, 0, 0, 0]
working_list = baselist # copies the object, not the elements inside

working_list[2] = "XX"
print(f"{working_list=}")
print(f"{baselist=}")
print()

# Note "baselist" has changed without explicitly doing so because it is the same object
# as working_list.

baselist = [0, 0, 0, 0]
working_list = baselist.copy()  # This is a copy and the two objects are different

working_list[2] = "XX"  # doesn't change baselist now.
print(f"{working_list=}")
print(f"{baselist=}")
Output:
working_list=[0, 0, 'XX', 0] baselist=[0, 0, 'XX', 0] working_list=[0, 0, 'XX', 0] baselist=[0, 0, 0, 0]
Reply


Messages In This Thread
RE: how can a variable change if I haven't changed it? - by bowlofred - Apr-07-2021, 05:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing two Pandas df’s and returning only changed records hobbycoder 1 706 Oct-29-2024, 01:55 PM
Last Post: deanhystad
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,413 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  The behavior of tune model has changed Led_Zeppelin 5 6,042 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  Change variable value during a while loop? penahuse 2 5,312 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 4,350 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  Change name of variable samuelbachorik 2 2,788 Aug-10-2020, 02:34 PM
Last Post: deanhystad
  Python - change variable type during program execution ple 1 3,267 Apr-12-2020, 08:43 AM
Last Post: buran
  I haven't a clue how to implement this logic 357mag 3 2,927 Apr-02-2020, 04:35 PM
Last Post: 357mag
  trying to change variable value with a def pythonbegginer 11 6,292 Mar-07-2020, 02:54 PM
Last Post: pythonbegginer
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 6,026 Jun-01-2019, 01:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

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