Python Forum
Variable being erased inside of if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable being erased inside of if statement
#1
On the program I'm currently working on I have a variable that disappears at an if statement and I can't figure out why. Here is sort of what the code looks like:

while (iteration_index < data_iterations):
	
    Do some stuff...

	for something in something_else:
        
        my_variable = something
	   
        Do more stuff...
        print(my_variable) # variable still there
		
        for something_else_2 in more_things:

		    filter_a_dataframe = an_unfiltered_dataframe
          
            print(my_variable) #still looks good
			
            if (not filter_a_dataframe.empty):
                print(my_variable) #my_variable has been erased here
				break
			else:
				continue
Oddly, if the if statement changes from
if (not filter_a_dataframe.empty) 
to
if (filter_a_dataframe.empty)
the variable doesn't get erased. Any ideas why this is happening? Also, I can provide the specific code if that helps. I was just trying to simplify it for easy reading.
Reply
#2
It is not possible. Something must happen in the part of the code that you don't show.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Jun-09-2024, 08:29 PM)Gribouillis Wrote: It is not possible. Something must happen in the part of the code that you don't show.

Should I post the full code?
Reply
#4
Also, print statements show the variable being preserved all the way up to the if statement.
Reply
#5
What do you mean by "disappeared"? What evidence do you have to come to your conclusion?

This is confusing.
Quote:if (not filter_a_dataframe.empty)
to
if (filter_a_dataframe.empty)
the variable doesn't get erased. Any ideas why this is happening? Also, I can provide the specific code if that helps. I was just trying to simplify it for easy reading.
When you remove the "not", the program flow changes. Statements that didn't run before now run. Is your proof that my_variable "disappeared" that nothing was printed? Maybe nothing was printed because filter_a_dataframe.empty is truthful.
Reply
#6
what error message do you get? does it say your variable (name) has disappeared? reappeared? is undefined? is transparent? can't be found? is hidden?

or do you get no error and just have wrong answers?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(Jun-09-2024, 11:50 PM)Skaperen Wrote: what error message do you get? does it say your variable (name) has disappeared? reappeared? is undefined? is transparent? can't be found? is hidden?

or do you get no error and just have wrong answers?

I finally figured it out. What was happening was when I ran the data through the if statement print statements kept putting out zero. As it turns out, there was an issue with how the files that didn't pass my filter were being saved downstream of the if statement. It cause my program to only save zeros to the file which were then passed back through the filter on subsequent iterations. The variable kept being printed fine because that was the variable I was comparing my saved files against. Sorry if that sounds a little confusing. This program is already 600 lines, so tracking down bugs gets a little weird.
Reply
#8
maybe you should not let your programs get that long. i've done that many times and big long programs also mean more variable names to keep track of. big programs should be more organized: functions, classes, modules.
Gribouillis and ndc85430 like this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Also, if you have not yet learnt about automated testing, then do. One writes code to exercise different parts of the application code to check it does the right thing - unit testing is an example of this. If done well, this kind of testing can give you safety when changing code as, since they're executable, you can execute them frequently and failures tell you that something is broken.

I first learnt about automated testing in the context of test-driven development. It's a process in which you write these sorts of tests _before_ writing the application code. There's a lot of literature on the subject, but at the least working this way helps you focus on the "what" you want the software to do, rather than the "how" and helps produce code that is easy to test in this automated fashion (as writing tests after the code has been written can be quite difficult for various reasons).
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 584 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 2,416 Nov-07-2023, 09:49 AM
Last Post: buran
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 1,631 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  An IF statement with a List variable dedesssse 3 8,896 Jul-08-2021, 05:58 PM
Last Post: perfringo
  define a variable using an if statement Margaridalopes 2 2,277 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  Correct syntax for a variable inside a quotes: MP4Box command JonnyDriller 2 2,875 Feb-02-2020, 01:22 AM
Last Post: JonnyDriller
  How to store the value from variable into list & run a statement then put in variable searching1 1 2,559 May-29-2019, 06:36 AM
Last Post: heiner55
  Get variable from class inside another class hcne 3 2,961 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  SELECT statement query question using a variable DT2000 2 3,159 Feb-23-2019, 07:35 AM
Last Post: DT2000
  Invalid syntax in two variable if statement? 00712411 1 2,360 Oct-10-2018, 12:36 AM
Last Post: stullis

Forum Jump:

User Panel Messages

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