Using Python eval to evaluate classes

How I can use eval to made class definitions inside other class ?

evalstr = str("class MyScreen(Screen):\n\tpass\n")
eval(evalstr)

I want to execute this code in other class method. But it returns an error.


You want exec not eval here (security concerns aside)

evalstr = str("class MyScreen(Screen):\n\tpass\n")
exec(evalstr)

eval will only evaluate an expression and return it's value. exec is used for executing arbitrary code strings as statements, which is what you need here.

But seriously this is a security disaster waiting to happen in most cases, consider alternatives (like writing the code not as a string).

EDIT: Chepner's answer is probably the right way to go.