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 6341341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:00:45+00:00 2026-05-24T20:00:45+00:00

For example, why does this work? def func1(func1var): def innerfunc(innerfuncvar): if func1var == 1:

  • 0

For example, why does this work?

def func1(func1var):
    def innerfunc(innerfuncvar):
        if func1var == 1:
             print innerfuncvar
        else:
             print 5
    func2(innerfunc)


def func2(function):
     function(9)

When innerfunc is called in func2, how does it know the values of func1var?

  • 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-24T20:00:46+00:00Added an answer on May 24, 2026 at 8:00 pm

    Python actually goes to some lengths to do this for you. When you define a function inside a function, you may use variables from the outer function in the inner function. This is true for any depth of nesting (that is, you can have a function inside a function inside a function inside a function… to any depth… and the innermost function can use variables from any of the enclosing functions). If there are conflicting names, the innermost variable with the name requested is used.

    What’s more, Python actually captures any variables you use from the outer function and stores them in the inner function, which is called a closure. So you can not only pass a function to another function, as you are doing, but you can return a function from a function, and variables you use from the outer function that were in effect when the function was defined will still be accessible from the returned function, even though the outer function isn’t running any more. This is a fairly advanced feature, but it lets you do something like this:

    def make_adder(increment):
        def adder(number):
            return number + increment
        adder.__name__ = "adder(%s)" % increment
        return adder
    

    This function is a function that creates a function that adds the specified value to it. For example:

    add1 = make_adder(1)
    add5 = make_adder(5)
    
    print add1(10)   # 11
    print add5(10)   # 15
    

    In this case, the value you pass to make_adder is captured and stored in the function that gets returned. This allows you to create a bunch of functions that add any number to their arguments. Which is a trivial example that isn’t actually very useful in real life, but serves to illustrate the feature.

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

Sidebar

Related Questions

I'm trying something like this, but this example does not work. jsObj = {};
I'm trying to understand why this does not work. (Basic example with no validation
if(strpos(http://www.example.com,http://www.)==0){ // do work} I'd expect this to resolve as true, which it does.
Does anybody have any example code on how this would work? Seems like it
How does len work on Python? Look at this example: class INT(int): pass class
What does this do? I find the example here, but other than what it
An example of an app that does this is Enso , it pops up
I see a lot of example code for C# classes that does this: public
Ryan Bates' nifty_scaffolding, for example, does this edit.html.erb <%= render :partial => 'form' %>
In this example from the App Engine docs , why does the example declare

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.