Duncan Booth wrote:
> Fuzzyman wrote:
[snip..]
>
> Does this do what you wanted? Instead of messing about with the code object
> just work out which values from the namespace the function actually
> expects.
>
> >>> def callfromnamespace(fn, namespace):
>     names = fn.func_code.co_varnames[:fn.func_code.co_argcount]
>     fn(**dict((name, namespace[name])
>         for name in names if name in namespace))
>
>
> >>> def f(x, y=99):
>     z = 2
>     print x, y, z
>
>
> >>> x = 42
> >>> callfromnamespace(f, globals())
> 42 99 2
> >>> y = 37
> >>> callfromnamespace(f, globals())
> 42 37 2
> >>> def testme():
>     x = 3
>     callfromnamespace(f, vars())
>     y = 9
>     callfromnamespace(f, vars())
>
>
> >>> testme()
> 3 99 2
> 3 9 2

Hmmm... it may do, thanks. :-)

I'll play around, and this may become a blog entry.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to