Python Forum

Full Version: Unrelated new function causing existing working function to fail
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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! :)
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.
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!