overiding assignment in module

Fredrik Lundh fredrik at pythonware.com
Tue Oct 25 08:09:15 EDT 2005


Viktor Marohnic wrote:

> I would to do something like this.
>
> container = []
>
> p1 = point()
> l1 = line()
>
> and i would like to override = method of the module so that its puts
> all objects into container.
> how i can do something like that.

you cannot, at least not as you've described the problem.  assignment is not
an operator in Python; it's a statement that modifies the current namespace.

things you can do include

- use an explicit target object

        x.p1 = point()
        x.l1 = line()

- execute the script in a controlled fashion, and inspect the resulting namespace:

        myscript = """
        p1 = point()
        l1 = line()
        """

        # create a new namespace, and add "built-in" functions to it
        namespace = {}
        namespace["point"] = point
        namespace["line"] = line

        # execute the script in this namespace
        exec myscript in namespace

        for key, item in namespace.iteritems():
            ...

</F> 






More information about the Python-list mailing list