**kwds behavior?

Jeff Epler jepler at unpythonic.net
Tue Sep 2 21:11:35 CEST 2003


On Tue, Sep 02, 2003 at 01:10:41PM -0400, Hans Nowak wrote:
> Now, on to the yucky solution:
> 
> >>> def g(**kwargs):
>         for key, value in kwargs.items():
>             exec "%s = %s" % (key, repr(value))
>         # test test...
> 	print x
[...]
Now, you don't need to do that.  This one works:
    def testKeywords5(**kwds):
        exec ""
        locals().update(kwds)
        print x

>>> testKeywords5(x=5)
5

even this variant should work (without actually executing any "exec"
statements):
    def testKeywords6(**kwds):
        locals().update(kwds)
        print x
        return
        exec ""

... but I don't think you can/should depend that either variation works.
The key is that when the compiler sees 'bare exec' it disables an
optimization used to turn local name accesses into fast C-array-based
indexing operations...

I stand by my words that you (the OP) should back up a step and choose a
solution that doesn't have this yucky requirement of locals that aren't
known at bytecompile time.

Jeff





More information about the Python-list mailing list