Python provides a few ways of executing it's own code. There are usually two Built-in Functions that allow us to execute python code from within python -:
- eval function -: The eval function can be used to execute any expression as incase is needed by you, but note that multiple lines worth of python code cannot be executed using eval.
- exec function -: The exec function can be used to execute multiple lines of python code also dynamically. It thus is different from the eval function due to it's ability to be able to execute more than just a python expression.
Now for your use case since you just want to execute a python expression stored in a string lets say e
the following can be done -:
e = '(2 *(4 - 3)) * 2' # Note that the previously written expression [2(4 - 3) * 2 is not a valid python expression as pointed out by @Brendan Abel]
result = eval(e) # The eval function executes the expression e and the result is stored in the result variable.
EDIT: So after your comment, your need seems to be to convert it into a normal expression that is at your disposal and can be run at any time.
For the same we can make a class called Expression, then we can make it store the expression as a .py file.
The expression object of the class can then be used to run it at anytime and also shall be disposed at the end to delete the .py file created initially.
The code for the same will look something like this -:
import importlib # Used for the Expression.run method.
import os # Used for the Expression.dispose method.
class Expression() :
def __init__(self, expr_name, str_expr) :
# This function is called at initialization of an Expression object.
self.expr_name = expr_name
self.str_expr = str_expr
self.module_obj = None # To store the module object returned by the import_module function.(defaultly None)
self.store() # Saves the expression as a .py file.
return
def store(self) :
# Creates a .py file with the name as that of the expression name to store
# the string expression for it to be later executed.
addr = self.expr_name + '.py'
with open(addr, 'w') as f :
f.write(self.str_expr)
return
def dispose(self) :
# Should be called either at the end of the program or whenever the use
# for the expression has ended and no further use is needed, this function
# deletes the .py file created initially to store the expression.
addr = self.expr_name + '.py'
os.remove(addr)
return
def run(self) :
# This function can be called to execute the expression and it imports
# the .py file previously created to indirectly execute it.
if self.module_obj == None :
# If the module_obj value is None it means the module is being
# imported for the first time.
self.module_obj = importlib.import_module(self.expr_name)
else :
# If the module_obj value is not None then the module has been
# imported before and thus is now reloaded using the previously
# stored module object.
self.module_obj = importlib.reload(self.module_obj)
return
pass
# The string expression.
e = 'print((2 * (4 - 3)) * 2)'
# Creating an expression object.
e_ = Expression('e1', e)
# Will print the result three times
e_.run()
e_.run()
e_.run()
# Now we dispose the expression since it is not needed anymore.
e_.dispose()
The output of the same is -:
4
4
4
After complete execution of program, all the used .py expression files are deleted if the expressions used have been disposed.