modifying locals

John [H2O] washakie at gmail.com
Thu Oct 30 17:21:01 EDT 2008


I would like to write a function to write variables to a file and modify a
few 'counters'. This is to replace multiple instances of identical code in a
module I am writing. 

This is my approach:

def write_vars(D):
    """ pass D=locals() to this function... """
    for key in D.keys():
        exec("%s = %s" % (key,D[key]))

    outfile.write(...)
    numcount += 1
    do this, do that...

the issue is that at the end, I want to return outfile, numcount, etc... but
I would prefer to not return them explicitly, that is, I would just like
that the modified values are reflected in the script. How do I do this?
Using global? But that seems a bit dangerous since I am using exec.

Bringing up another matter... is there a better way to do this that doesn't
use exec?

Thanks!

-- 
View this message in context: http://www.nabble.com/modifying-locals-tp20255725p20255725.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list