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

  • Home
  • SEARCH
  • 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 5936673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:25:56+00:00 2026-05-22T15:25:56+00:00

So AFAIK in CPython, function definitions are compiled into function objects when executed at

  • 0

So AFAIK in CPython, function definitions are compiled into function objects when executed at parse time. But what about inner functions? Do they get compiled into function objects at parse time or do they get compiled (or interpreted) every single time the function is called? Do inner functions incur any performance penalty at all?

  • 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-22T15:25:56+00:00Added an answer on May 22, 2026 at 3:25 pm

    To give a general explaination – assuming you have the following code in a module:

    def outer(x=1):
        def inner(y=2):
            return x+y
    

    When the file is parsed by python via compile(), the above text is turned into bytecode for how to execute the module. In the module bytecode, there are two “code objects”, one for the bytecode of outer() and one for the bytecode inner(). Note that I said code objects, not functions – the code objects contain little more than the bytecode used by the function, and any information that could be known at compile time – such as the bytecode for outer() containing a ref to the bytecode for inner().

    When the module actually loads, by evaluating the code object associated with the module, one thing which happens is an actual “function object” is created for outer(), and stored in the module’s outer attribute. The function object acts as a collection of the bytecode and all context-relavant things that are needed to call the function (eg which globals dict it should pull from, etc) that can’t be known at compile time. In a way, a code object is a template for a function, which is a template for execution of the actual bytecode with all variables filled in.

    None of this involved inner()-as-a-function yet – Each time you actually get around to calling outer(), that’s when a new inner() function object is created for that invocation of outer, which binds the already-created inner bytecode object to a list of globals, including the value of x as passed into that call to outer. As you can imagine, this is pretty fast, since no parsing is needed, just filling in a quick struct with some pointers to other already-existing objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

AFAIK profilers can only tell how much time is spent in each function. But
AFAIK: C++ provides three different types of polymorphism. Virtual functions Function name overloading Operator
AFAIK, we can have two static variables with the same name in different functions?
AFAIK SQLite returns unicode objects for TEXT in Python. Is it possible to get
AFAIK, the JQuery ID selector: var ctlId = $('#' + strControlId); gets translated into
AFAIK there is no DOS in vista and windows 7, but I need to
i was wondering how programs like ccleaner and utorrent are made? AFAIK they are
AFAIK, a thread can not access UI components. But I want to preload images
I know C# is getting a lot of parallel programming support, but AFAIK there
AFAIK, usual ASP.NET web site/web application switched on into DEBUG mode when web/app-config setting

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.