Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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...

like image 548
Jay Avatar asked Jan 06 '20 23:01

Jay


1 Answers

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
like image 161
gold_cy Avatar answered Oct 19 '22 14:10

gold_cy