modifying locals

John [H2O] washakie at gmail.com
Thu Oct 30 19:19:11 EDT 2008




Steven D'Aprano-7 wrote:
> 
> What you are actually trying to do is unclear to me. Perhaps you could 
> try explaining better with a more concrete example?
> 
> -- 
> Steven
> --
> 

Actually, maybe a LACK of an example would make it simpler. What I'm after
is a function, to which I can pass a dictionary defined from locals(), then
in the function I would modify some of the variables from the dictionary.
But I want the modifications to be 'seen' by the method that called the
function without passing them back via return.

Ideally, I would prefer not to use global, as I think (due to other problem
in my scripting) this might cause problems.

Currently I these two possibilities:

def myFunction(D):
    for key,item in D.iteritems():
        exec "%s = %s" % (key, item) 

     modify a few of the elements...
     return locals()

then,
#script
D=locals()
D=myFunction(D)
for key,item in D.iteritems():
   exec "%s = %s" % (key,item)


OR:

def myFunction(D):
    for key,item in D.iteritems():
        exec "%s = %s" % (key, item) 

     modify a few of the elements...
     declare global on the elements modified...


then,
#script
D=locals()
myFunction(D)

As a point.. I thought I read somewhere that D.iteritems wasn't going to be
available in Python3 so I've been trying to 'ween' myself from it.

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




More information about the Python-list mailing list