Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Mar 2012
    Posts
    3,284

    Default Python query - unrelated new function breaking existing function

    Hi all,

    I have a python script that basically works as follows:

    Code:
    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":

    Code:
    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!

  2. #2
    Senior Member
    Join Date
    Sep 2007
    Location
    Lower Hutt, NZ
    Posts
    706

    Default Re: Python query - unrelated new function breaking existing function

    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 you’re just guessing. Or remove the except entirely and see why the compiler crashes.

  3. #3
    Senior Member MushHead's Avatar
    Join Date
    Jun 2006
    Posts
    493

    Default Re: Python query - unrelated new function breaking existing function

    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.
    There are 10 types of people in this world - those who understand binary, and those who don't.

  4. #4
    Senior Member
    Join Date
    Sep 2007
    Location
    Lower Hutt, NZ
    Posts
    706

    Default Re: Python query - unrelated new function breaking existing function

    Quote Originally Posted by MushHead View Post
    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:
    Code:
    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
    Interests: PVRs, HTPCs, Apple IOS, Apple TV, Media Players, C#, Printing

  5. #5
    Senior Member
    Join Date
    Mar 2012
    Posts
    3,284

    Default Re: Python query - unrelated new function breaking existing function

    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 - https://python-forum.io/Thread-Unrel...nction-to-fail

    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
    Code:
    return asnwer
    instead of
    Code:
    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
    Last edited by Nick G; 28-01-2020 at 09:05 AM.

Similar Threads

  1. Excel XP: Countif function query
    By tinakarori in forum PressF1
    Replies: 3
    Last Post: 10-06-2007, 11:16 PM
  2. Laptop Function keys don't function
    By Greg in forum PressF1
    Replies: 2
    Last Post: 24-12-2006, 09:13 PM
  3. XL Function
    By B.M. in forum PressF1
    Replies: 11
    Last Post: 23-07-2006, 08:25 PM
  4. ALT Function on F1
    By SurferJoe46 in forum PC World Chat
    Replies: 10
    Last Post: 02-04-2006, 04:38 PM
  5. MS Dos Function
    By waituke in forum PressF1
    Replies: 5
    Last Post: 08-04-2004, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •