Conditionals stored as text to translate to real compares

Mike Meyer mwm at mired.org
Thu Jan 12 19:57:30 EST 2006


"Randall Parker" <techiepundit at futurepundit.com> writes:
> if OperatorType == ">":
>    # then do a greater than compare here.
>    BoolVal = TestVal > TargetVal
> elif OperatorType == ">=":
>    # then do a greater or equal to here.
>    BoolVal = TestVal >= TargetVal
>  and so on.
>
> It would seem a lot easier to do:
>
>     BoolVal  = TargetVal EvalStrToOper(">") MyInnerList(2)
>
>  where EvalStrToOper creates an immediate > sign that then allows that
> line to get evaluated by the interpreter.


How about:

    BoolVal = EvalStrToOper[">"](TargetVal, MyInnerList(2)) ?

EvalStrOper = {'>': operator.gt, '>=': operator.ge,
               '<': operator.lt, '<=': operator.le,
               ...

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list