How safe is modifying locals()?

Ian Bicking ianb at
Sat Jul 26 00:55:32 CEST 2003

On Fri, 2003-07-25 at 12:28, Paul Paterson wrote:
> Thanks Terry! I adjusted my tests and now see this behaviour exactly. 
> The function itself works but the changes do not propogate out to the 
> calling scope. So this approach of using locals() appears to be dead.
> Are there any other approaches?

Think about what you are trying to do, and try to identify a (mutable)
object that can encapsulate that.  Then pass the object, and modify its
instance variables, like:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

def change(p):
    p.y = 10

obj = Point(0, 0)
assert obj.y == 10

Maybe this mutable object will simply be the main application object,
and instead of functions you will use methods of that application


More information about the Python-list mailing list