Velocity Reviews > assign operator as variable ?

# assign operator as variable ?

s99999999s2003@yahoo.com
Guest
Posts: n/a

 06-07-2006
hi
in python is there any way to do this

op = "<"
a = 10
b = 20
if a op b :
print "a is less than b"

??

thanks

-- bj0rn
Guest
Posts: n/a

 06-07-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hi
> in python is there any way to do this
>
> op = "<"
> a = 10
> b = 20
> if a op b :
> print "a is less than b"

(E-Mail Removed) wrote:
> hi
> in python is there any way to do this
>
> op = "<"
> a = 10
> b = 20
> if a op b :
> print "a is less than b"

Will this work for you?:

import operator
op = operator.lt
a = 10
b = 20
if op(a, b):
print "a is less than b"

-- bj0rn

Fredrik Lundh
Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) wrote:

> in python is there any way to do this
>
> op = "<"
> a = 10
> b = 20
> if a op b :
> print "a is less than b"
>
> ??

the "operator" module contains functions corresponding to all builtin
operators:

import operator

ops = {
"==": operator.eq,
"!=": operator.ne,
"<>": operator.ne,
"<": operator.lt,
"<=": operator.le,
">": operator.gt,
">": operator.ge
}

op = "<"

a = 10
b = 20

if ops[op](a, b):
print "a is less than b"

</F>