eval
can take a dictionary as its second argument which contains a new environment. Create a dictionary env
and set your new variables within there, which ensures it won't clash with your local namespace:
def CheckConstraints(self, Selections):
'Validate the stored constraints'
Good = True
env = {}
## Undefined options default to False
for i in self.Options:
env[i] = False
## Set defined Options to True
for i in Selections:
env[i] = True
for i in self.Constraints:
if not eval(i, env):
Good = False
print "Constraint Check Failure:", i, Selections
else:
print "Constraint Check OK:", i, Selections
return Good