Anonymus functions revisited
bruno modulix
onurb at xiludom.gro
Wed Mar 23 04:50:33 EST 2005
George Sakkis wrote:
> "Ron" <radam2 at tampabay.rr.com> wrote:
>
>>On Tue, 22 Mar 2005 21:56:57 GMT, Ron <radam2 at tampabay.rr.com> wrote:
>>
>>
>>>Why should a function not create a local varable of an argument if the
>>>varable doesn't exist and a default value is given?
>>
>>ok... thought it out better. :)
>>
>>Getting a default into a function isn't the problem. Returning the
>>value to a varable that doesn't exist is.
>>
>>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"
>
Interesting. I'll keep a copy of this one in my cookbook for further
exploration. But I think I would use such a thing in production code.
>>> b = 25
>>> makeVars(b=88)
>>> b
88
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list