MakaMaka wrote: > I have a scope related question that I haven't been able to find an > answer to anywhere. Is there a way to have a function in an imported > module add variables to the scope of the calling script? Basically, > can I have the following: > > #root.py > import some_module.py > > some_module.afunction() # <== create a variable x1=1 and add it to the > scope of root.py > > print x1 # <== would print 1 > > I know this can be accomplished by passing the what globals() returns > into the function; however, I'm wondering if there is a way to do this > without passing anything to the function.
Try: x1 = some_module.afunction() This has the advantage that in some other script you can decide to give the variable a meaningful name, and it doesn't have to be the same as the name you used in your first script. You can easily extend this technique to set several variables in the calling script: x1, y2, z3 = some_module.afunction() You don't even have to restrict yourself to global variables, this technique will even work to let the function set variables in another function's local namespace or attributes on some arbitrary object. -- http://mail.python.org/mailman/listinfo/python-list