Python Forum

Full Version: Variable being erased inside of if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
It is not possible. Something must happen in the part of the code that you don't show.
(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?
Also, print statements show the variable being preserved all the way up to the if statement.
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.
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?
(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.
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.
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).