George Sakkis wrote:

>> So then the question is ... is there a way for a function to create a
>> varable in it's parents namespace that persists after the function is
>> done?
> 
> Yeap.. a simple one-liner can do the trick:
> 
> def makeVars(**nameVals):
>     sys._getframe(1).f_locals.update(nameVals)
> 
> try: b
> except NameError: print "Before makeVars: NameError"
> else: print "Before makeVars: Not NameError"
> makeVars(b=2)
> try: b
> except NameError: print "After makeVars: NameError"
> else: print "After makeVars: Not NameError"

Do I really need to mention that the whole concept here is broken. This 
only works if you call it from global scope. If you call it from inside a 
function it [usually] won't work:

>>> def makeVars(**nameVals):
    sys._getframe(1).f_locals.update(nameVals)

    
>>> def test():
        try: b
        except NameError: print "Before makeVars: NameError"
        else: print "Before makeVars: Not NameError"
        makeVars(b=2)
        try: b
        except NameError: print "After makeVars: NameError"
        else: print "After makeVars: Not NameError"

        
>>> import sys
>>> test()
Before makeVars: NameError
After makeVars: NameError
>>> 
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to