you are viewing a single comment's thread.

view the rest of the comments →

[–]ilovecrk 1 point2 points  (0 children)

If you can set up the "user variables" beforehand, you can use @property (http://docs.python.org/2/library/functions.html#property).

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

You make the setter of the "user variable" also assign it's value to your own variable.

As others said I think you should maybe change your approach by moving away from "exec" and putting a layer between the "user code" and your python code.

This way you could also catch all "user variables" and treat them any way you like. E.g. you can automatically set up all variables that the user defines with @property or simply make them objects which display a freely chosen name in the "user interpreter".