Feedback on implementation of function which compares integer signs in Python.

Posted by John Magistr on Stack Overflow See other posts from Stack Overflow or by John Magistr
Published on 2010-12-29T22:13:56Z Indexed on 2010/12/29 22:54 UTC
Read the original article Hit count: 118

Filed under:

Hi all.

I've made a small function which, given a tuple, compares if all elements in this tuple is of the same sign.

E.g., tuple = [-1, -4, -6, -8] is good, while [-1, -4, 12, -8] is bad. I am not sure I've made the smartest implementation, so I know this is the place to ask.

def check_consistent_categories(queryset):
try:
    first_item = queryset[0].amount

    if first_item < 0:
        for item in queryset:
            if item > 0:
                return False
        return True
    else:
        for item in queryset:
            if item < 0:
                return False
        return True
except:
    return False

© Stack Overflow or respective owner

Related posts about python