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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:02+00:00 2026-05-13T09:01:02+00:00

I asked previously how the nested functions work, but unfortunately I still don’t quite

  • 0

I asked previously how the nested functions work, but unfortunately I still don’t quite get it. To understand it better, can someone please show some real-wold, practical usage examples of nested functions?

Many thanks

  • 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-13T09:01:02+00:00Added an answer on May 13, 2026 at 9:01 am

    Your question made me curious, so I looked in some real-world code: the Python standard library. I found 67 examples of nested functions. Here are a few, with explanations.

    One very simple reason to use a nested function is simply that the function you’re defining doesn’t need to be global, because only the enclosing function uses it. A typical example from Python’s quopri.py standard library module:

    def encode(input, output, quotetabs, header = 0):
        ...
        def write(s, output=output, lineEnd='\n'):
            # RFC 1521 requires that the line ending in a space or tab must have
            # that trailing character encoded.
            if s and s[-1:] in ' \t':
                output.write(s[:-1] + quote(s[-1]) + lineEnd)
            elif s == '.':
                output.write(quote(s) + lineEnd)
            else:
                output.write(s + lineEnd)
    
        ...  # 35 more lines of code that call write in several places
    

    Here there was some common code within the encode function, so the author simply factored it out into a write function.


    Another common use for nested functions is re.sub. Here’s some code from the json/encode.py standard library module:

    def encode_basestring(s):
        """Return a JSON representation of a Python string
    
        """
        def replace(match):
            return ESCAPE_DCT[match.group(0)]
        return '"' + ESCAPE.sub(replace, s) + '"'
    

    Here ESCAPE is a regular expression, and ESCAPE.sub(replace, s) finds all matches of ESCAPE in s and replaces each one with replace(match).


    In fact, any API, like re.sub, that accepts a function as a parameter can lead to situations where nested functions are convenient. For example, in turtle.py there’s some silly demo code that does this:

        def baba(xdummy, ydummy):
            clearscreen()
            bye()
    
        ...
        tri.write("  Click me!", font = ("Courier", 12, "bold") )
        tri.onclick(baba, 1)
    

    onclick expects you to pass an event-handler function, so we define one and pass it in.

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

Sidebar

Related Questions

No related questions found

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.