반응형

상황

스트링으로 되어있는 비교 연산자를 사용해서 비교를 할 수 있을까?

 

해결

import operator


def check(left, string_operator, right):
    operators = {'>': operator.gt,
                 '<': operator.lt,
                 '>=': operator.ge,
                 '<=': operator.le,
                 '=': operator.eq}
    return operators[string_operator](left, right)


print(check(3, '>', 2))  # True
print(check(3, '<', 2))  # False
print(check(3, '>=', 2))  # True
print(check(3, '<=', 2))  # False
print(check(3, '=', 2))  # False

 

반응형

+ Recent posts