Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > assign operator as variable ?

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
-- 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

 
Reply With Quote
 
 
 
 
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is it not possible to assign a global variable to anotherglobal variable..? Jase Schick C Programming 11 08-05-2010 05:16 AM
assign JavaScript variable to Java variable problem in JSP Matt Java 8 01-25-2010 04:48 PM
How to Assign JavaScript variable to Jsp Variable Ranjan ASP .Net 1 04-10-2007 03:35 PM
assign a struct variable into another struct variable ineedyourluvin1@yahoo.com C++ 18 07-22-2005 04:08 PM
Assign a name to a pointer variable from another variable Nmishra C Programming 3 05-24-2004 03:57 PM



Advertisments