Python Forum
Unrelated new function causing existing working function to fail
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unrelated new function causing existing working function to fail
#1
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, everything works perfectly.

Any help or tips on where to look would be appreciated! :)
Reply
#2
Without seeing the "Does some stuff" code, I'm going to guess the core problem is on line 19:

Answers.append(FunctionA(items))
Why send the entire list to the function after checking a single item in the list. Plus, the function signature uses "item" suggesting only a singular value rather than a list of values.

Also, what's with the parentheses on the return statements?

***

Edit: You can also change your except clause to something more descriptive:

except Exception as e:
    print(e)
Or you could remove the except clause to see the full stack trace.
Reply
#3
Ok, I've narrowed it down a little.

Firstly, passing through items rather than item was a typo when writing the forum post. The code itself does use item.
The brackets around the return statement were a bad coding habit of mine. They've now been removed.

Update.... I'm clinically dumb. I removed the error handling as you suggested, and got back

'function' object is not subscriptable
Worked back through and had a look at it, and I'd given FunctionB an identical name to a variable in FunctionA, hence the (in hindsight rather obvious) error. A quick fix thankfully!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 608 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 614 Oct-18-2023, 11:09 PM
Last Post: Edward_
Shocked kindly support with this dropna function not working gheevarpaulosejobs 2 662 Jul-24-2023, 03:41 PM
Last Post: deanhystad
  why does VS want to install seemingly unrelated products? db042190 3 631 Jun-12-2023, 02:47 PM
Last Post: deanhystad
  How to calculated how many fail in each site(s) in csv files SamLiu 4 1,281 Sep-26-2022, 06:28 AM
Last Post: SamLiu
Exclamation Function Not Working Alivegamer 7 1,862 Jul-19-2022, 01:03 PM
Last Post: deanhystad
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,585 Mar-11-2022, 01:50 PM
Last Post: snippsat
  try function working on PC but not raspberry pi AnotherSam 1 1,525 Oct-11-2021, 04:51 AM
Last Post: bowlofred
  Exit function from nested function based on user input Turtle 5 2,890 Oct-10-2021, 12:55 AM
Last Post: Turtle
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,451 Aug-22-2021, 06:59 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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