How do I modify my callers local variables?

Andreas Dietrich andreas.dietrich at news.online.de
Mon Feb 12 15:27:10 EST 2001


Hallo, Pythoneers!

I tried to simplify keyword parsing and wrote the following routine:

def parse_keywords(defaults,kw):
    import sys
    try:
        raise None
    except:
        frame = sys.exc_info()[2].tb_frame.f_back
        locals_dict=frame.f_locals
    for i in defaults.keys():
        try:
            locals_dict[i]=kw[i]
        except KeyError:
            try:
                locals_dict[i]=defaults[i]
            except KeyError:
	        #We should not be here
                print 'Moo. Moo'
    return

def test(**kw):
    parse_keywords({'a':1,'b':2,'c':3},kw)
    #didn't work
    print a,b,c

test()

Unfortunately it doesn't work.
Is there a way to do it? 
I'm tired of the repetitious code involved in keyword parsing.

I'd really appreciate any insights into this matter.

Thank you. Andreas

-- 
"We've heard that a million monkeys at a million keyboards could produce the
complete works of Shakespeare. Now, thanks to the Internet, we know this is not
true."  -- Robert Wilensky




More information about the Python-list mailing list