开发者

How can I call a inner function from the Python shell?

开发者 https://www.devze.com 2023-01-17 03:17 出处:网络
I have some code (that I can\'t easily modify), of the following form: def foo(x): do_other_stuff_that_I_do_not_want_to_do()

I have some code (that I can't easily modify), of the following form:

def foo(x):
   do_other_stuff_that_I_do_not_want_to_do()
   def bar():
      "do something"
      str(x)
   bar()

I would like to call bar(), directly, from the Python shell. I don't mind using co_g开发者_C百科lobals, or other internal bits. I have the feeling that this may be impossible; is it?


It is impossible to get at the inner function object with the code as you've stated it -- said object is only created (by the def statement) when the outer function runs (i.e., when it gets called).

As an aside, note that outer functions like foo are often coded to return the inner function as their result (e.g. by changing bar() to return bar) as the last line, exactly to work as "function factories" (often, as "closure factories") rather than keep the very existence of an internal function as a kind of private, secret implementation detail; but your coding picks the latter route instead.

Edit...:

It is possible to get at the code object for the inner function, however:

>>> def outer():
...   def inner(): return 'boo'
...   print inner()
... 
>>> eval(outer.func_code.co_consts[1])
'boo'
>>> 

However, in general, to make a code object into a callable function requires considerable work (in this special case an eval suffices because the inner function has no arguments nor any nonlocal variables, but of course that's not anywhere even close to the general case... in which you do have to supply such niceties as bindings for nonlocals!-)

0

精彩评论

暂无评论...
验证码 换一张
取 消