Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:22:14+00:00 2026-05-16T22:22:14+00:00

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

  • 0

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_globals, or other internal bits. I have the feeling that this may be impossible; is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:22:15+00:00Added an answer on May 16, 2026 at 10:22 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that is using SyncEnumerator. As you can see here ,
Can anyone help? I have some code that is shared between 2 projects. The
Logging can get complicated, quickly. Considering that you have some code, how do you
I have some code that can convert a single color in a template image
I have some code that can spawn background worker threads from anywhere within it.
I have some code that looks something like this: <head runat="server"> <script type="text/javascript"> var
I have some Python code that looks something like this: rates = {3: [((17500,
I have some code that is executed when the process is killed, can I
So I have some code that can read the methods out of a .coverage
I have some original code that manages exception safety like this: void foo() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.