Jay
Jay

Reputation: 1309

How to pass a greater than or less than sign through a parameter?

If it possible to pass a >= sign through a parameter: As in:

def example():
   num1 = 0
   num2 = 5
   sign1 = >=
   sign2 = <=

   test(num1, num2, sign1, sign2)

def test(num1, num2, sign1):
    while num1 sign1 0 and num2 sign2 5:
       print('whatever')
       num1+=1
       num2-=1

Obviously this isn't what I'm really trying to do; I was just wondering if it was possible...

Upvotes: 10

Views: 4469

Answers (1)

gold_cy
gold_cy

Reputation: 14216

Yes it is possible using the operator module. However they are treated as functions and not strings which is what they appear to be in your original attempt.

from operator import le, ge

def example():
   num1 = 0
   num2 = 5
   sign1 = ge
   sign2 = le


def test(num1, num2, sign1, sign2):
    while sign1(num1, 0) and sign2(num2, 5):
       print('whatever')
       num1+=1
       num2-=1

Upvotes: 10

Related Questions