Inferring initial locals()

Scott David Daniels scott.daniels at acm.org
Sun Jun 24 22:52:26 EDT 2007


George Sakkis wrote:
> On Jun 21, 4:42 pm, "bruno.desthuilli... at gmail.com"
> <bruno.desthuilli... at gmail.com> wrote:
>> On Jun 21, 8:51 pm, George Sakkis <george.sak... at gmail.com> wrote:
>>
>>> I wonder if there is a (preferably not too-hackish) solution to the
>>> following introspection problem: given a callable and a number of
>>> positional and/or keyword arguments, infer what would be the frame's
>>> locals() right after the function is called....
> 
> Turns out it wasn't that hard after all; I came up with the following:
> ....

So, what should your code do about this:

def someFunction(a, b, c=43, d=14, f=12):
     print locals()

import functools

a_funct = functools.partial(someFunction, d=13, c=5)
b_funct = functools.partial(a_funct, 14, d=12)
localsProber(b_funct)

My point is simply:
Introspection code is often written with a fixed idea of what other
programmers write.  The others don't use introspection, the others
don't use higher order functions to build functions, ....

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list