| Forum Home | ||||
| PC World Chat | ||||
| Thread ID: 148488 | 2020-01-27 08:08:00 | Python query - unrelated new function breaking existing function | Nick G (16709) | PC World Chat |
| Post ID | Timestamp | Content | User | ||
| 1466275 | 2020-01-27 08:08:00 | Hi all, I have a python script that basically works as follows: Items = pd.Dafaframe(some excel sheet) def FunctionA(items): try: """Does some stuff""" return(answer) except: return("Fuction A error") Answers = [] for item in Items: if item["Attribute"] == "A": Answers.append(FunctionA(items)) elif item["Attribute"] == "B": Answers.append("Attirubte type is B") else: return("Error") This works perfectly fine and works exactly as expected. The "Function A error" is never returned (as it shouldn't be, it's purely there to catch any unexpected exception. If I choose to develop a Function to Handle Type 4, for instance changing my code to the following, every single time Function A is called it returns "Function A error": Items = pd.Dafaframe(some excel sheet) def FunctionA(item): try: """Does some stuff""" return(answer) except: return("Fuction A error") def FunctionB(item): return("Attribute type is B") Answers = [] for item in Items: if item["Attribute"] == "A": Answers.append(FunctionA(items)) elif item["Attribute"] == "B": Answers.append(FunctionB(item)) else: return("Error") Any thoughts on why this might be the case? I'm very confused for the following reasons: Function A is still being called, as I'm getting function A error Function B really only does return a string - so it doesn't adjust any variable that function A might use WHen I put the return statement directly into the elif statement as an append (ie, directly appending the string answer), everything works perfectly. Any help or tips on where to look would be appreciated! :) |
Nick G (16709) | ||
| 1466276 | 2020-01-27 17:45:00 | What is function a doing to get an error? Or what is the error? Change the except to except Exception as e And then print the e to se what the error is, otherwise youre just guessing. Or remove the except entirely and see why the compiler crashes. |
psycik (12851) | ||
| 1466277 | 2020-01-27 19:35:00 | Is your code above directly copied from source? Because there's references to 'Items', 'items' & 'item' in there, only 2 of which seem to exist. Also, in Python, return "Hello" is NOT the same as return ("Hello"). return is a statement, so the former returns a string, and the latter returns a 1-element tuple containing a string - this will result in behaviour you don't expect. I'm guessing if you take up psycik's suggestion of removing the except clause, you'll probably see exceptions related to this. | MushHead (10626) | ||
| 1466278 | 2020-01-27 19:48:00 | Is your code above directly copied from source? Because there's references to 'Items', 'items' & 'item' in there, only 2 of which seem to exist. Also, in Python, return "Hello" is NOT the same as return ("Hello"). return is a statement, so the former returns a string, and the latter returns a 1-element tuple containing a string - this will result in behaviour you don't expect. I'm guessing if you take up psycik's suggestion of removing the except clause, you'll probably see exceptions related to this. Yeah good catch: if item["Attribute"] == "A": Answers.append(FunctionA(items)) items doesn't exists....it's Items. But you should do something in the exception as I suggested and you should see functionA complaing about trying to do something on a NoneType |
psycik (12851) | ||
| 1466279 | 2020-01-27 20:03:00 | Good spot on the item/Items/items. That was me being a bit sloppy and careless when writing the post. It wasn't actual code, and all actual code referred to item as it should. I've found the issue - as you said, I should have removed let the error run so I could see what it was. As is often the case, a very simply mistake I really shouldn't have made and definitely should have caught on my own! The actual name of FunctionB was identical to the name of a variable I had used in FunctionA, leading to the error. A good lesson for me to be more careful and systematic with how I name functions and variables, especially when combining two files into one. If anyone is interested, this was also asked here - python-forum.io Thanks all for the help. Apologies about it clearly being pseudocode - the actual code is work related so confidential, so that was the best I could do. edit - as was also pointed out, I should have used return asnwer instead of return(answer). I've now fixed this - although it was not the cause of the issue and either way worked fine, it was still bad code. I'ts been a while since I used python :) |
Nick G (16709) | ||
| 1 | |||||