-1

I am trying to execute a String that contains Python code as below. The Python's features I am implementing to run the String containing Python code are eval and exec

from CodeExecs.Compilers import Code1, Code2, Code3, Code4, Code5, Code6, Code7, Code8, Code9, Code10, Code11, Code12, Code13, Code14, Code15, Code16, Code17, Code18, Code19, Code20
program = """for i in range(1, 21):\n\tif eval(f'Code{i}().load(jobj)'): return 'True'\n\telse: return 'False'"""
print(exec(program))

If the for-loop in program appears obscure, it could be found in the error message at the bottom. The above code prints True when the case is satisfied (Code1 & Code2). The expression in eval raises an exception for Code3.

So instead of printing, if I try to return True / False as below:

program = """for i in range(1, 21):\n\tif eval(f'Code{i}().load(jobj)'): return 'True'\n\telse: return 'False'"""
print(exec(program))

I see an error:

SyntaxError: 'return' outside function

Below is the full console log with error message.

for i in range(1, 21):
    if eval(f'Code{i}().load(jobj)'): return 'True'
    else: return 'False'
Traceback (most recent call last):
  File "C:/CodeValidation/CheckSchema.py", line 22, in <module>
    print(exec(program))
  File "<string>", line 2
SyntaxError: 'return' outside function

Could anyone let me know what is the mistake I am doing here and how can I return True or False in my case as given above? Any help is much appreciated.

6
  • Does this answer your question? Python return eval value within function? Commented Jun 23, 2021 at 14:20
  • 1
    Why are you using eval? Just put the 20 Code classes in a list: for c in [Code1, ..., Code20]: if c().load(obj): return 'True'. Commented Jun 23, 2021 at 14:21
  • Also, once you return anything, the loop will exit if it's inside a function Commented Jun 23, 2021 at 14:22
  • As the error points out: Your code string isn't embedded in a function from which it could return something. And as @LuisAFK pointed out: Even if it were, the loop would stop in the first iteration because of the return. Commented Jun 23, 2021 at 21:23
  • @Timus & LuisAFK, I recently started Python and got to know the availability of this feature the day I posted this question. Hence the confusion. Commented Jun 24, 2021 at 6:16

1 Answer 1

1

You can just store your classes in a list to iterate over.

from CodeExecs.Compilers import Code1, Code2, Code3, Code4, Code5, Code6, Code7, Code8, Code9, Code10, Code11, Code12, Code13, Code14, Code15, Code16, Code17, Code18, Code19, Code20


def something():
    return all(c().load(jobj) for c in [Code1, Code2, ..., Code20])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.