0

I tried to create a small function using the exec() command. I works fine and the string in the exec() command gets assembled correctly.

The function:

def get_random_persons_by_attributes(self, attributes, k):
        def pass_check(person):
            passed = True
            for att in attributes:
                exec(f"if not person.{att['attribute']} {att['operator']} {att['value']}: passed = False")
            return passed
            
        print(pass_check(self.residents[0]))

The function call:

get_random_persons_by_attributes([{"attribute": "age[\"age\"]", "operator": "==", "value": 19}], 1)

Everything works fine and I get no errors running the function, but the value of “passed“ does not get changed, even if the conditions are met. How do I fix this?

0 Answers0