I am trying to define an object masterwhose attribute / method decidecan be called with an expression ex, but the evaluation of this expression is delayed until some logic in decideasks for it (and this may not even do that). So, in code like:
master.decide(ex)
it should be possible to exremain unappreciated, if logic says so decide. In code, it exmeans "an arbitrary expression provided by my library client." It can be as simple as 1 + 2(where I wouldn’t care about a lazy assessment) to as complicated as do_http_request().delete_record_from_database()(where I certainly don’t care).
Is this possible? I am flexible on the syntax side, so if there is a solution that should include additional operators, etc. Around ex, I can consider it. But I would like to exremain an expression.
I was thinking about (ab) using short circuit operators such as andand or, but there seems to be no way to get them to return something other than a boolean.
The best I could think of was to wrap exin lambda:
master.decide(lambda: ex)
def decide(ex):
if decide_to_do_it():
result = ex()
somehow_use(result)
Or passing it as a string and using eval:
master.decide('ex')
def decide(ex):
if decide_to_do_it():
result = eval(ex)
somehow_use(result)
(Yes, this would have problems defining the scope).
Perhaps there is a magic function, a trick, or something else that would allow me to save exas a simple expression?