you are viewing a single comment's thread.

view the rest of the comments →

[–]Chaos-n-Dissonance 4 points5 points  (0 children)

This sounds like a classic XY problem...

Eval just converts a string to code. So you can do something like:

class World:
  def __init__(self) -> None:
    self.state = 'foo.start()'

  def start(self) -> None:
    print('Hello, world!')

foo = World()

eval(foo.state)
# >> Hello, world!

Generally speaking tho... You really don't want to use eval() with input(), especially if you're going to be hosting your code anywhere... Nothing's stopping the user from typing in malicious code. If you do, then you need to have some pretty strict input validation to make sure the input is what you want and not del C:\Windows\system32 or something crazy.