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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:12:35+00:00 2026-05-14T19:12:35+00:00

I am learning Python using Dive Into Python 3 book. I like it, but

  • 0

I am learning Python using Dive Into Python 3 book. I like it, but I don’t understand the example used to introduce Closures in Section 6.5.

I mean, I see how it works, and I think it’s really cool. But I don’t see any real benefit: it seems to me the same result could be achieved by simply reading in the rules file line by line in a loop, and doing search / replace for each line read.

Could someone help me to:

  • either understand why using closures in this example improves the code (e.g., easier to maintain, extend, reuse, or debug?)

  • or suggest a source of some other real-life code examples where closures really shine?

  • 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-14T19:12:36+00:00Added an answer on May 14, 2026 at 7:12 pm

    Decorators are an example of closures. For example,

    def decorate(f):
        def wrapped_function():
            print("Function is being called")
            f()
            print("Function call is finished")
        return wrapped_function
    
    @decorate
    def my_function():
        print("Hello world")
    
    my_function()
    

    The function wrapped_function is a closure, because it retains access to the variables in its scope–in particular, the parameter f, the original function. Closures are what allow you to access it.

    Closures also allow you to retain state across calls of a function, without having to resort to a class:

    def make_counter():
        next_value = 0
        def return_next_value():
            nonlocal next_value
            val = next_value
            next_value += 1
            return val
        return return_next_value
    
    my_first_counter = make_counter()
    my_second_counter = make_counter()
    print(my_first_counter())
    print(my_second_counter())
    print(my_first_counter())
    print(my_second_counter())
    print(my_first_counter())
    print(my_second_counter())
    

    Also, bound methods are technically closures (though they’re probably implemented differently). Bound methods are class member functions with their class baked in:

    import sys
    w = sys.stdout.write
    w("Hello\n")
    

    w is essentially a closure with a reference to the sys.stdout object.

    Finally, I haven’t read that book, but a quick read of the chapter you linked and I’m very unimpressed–it’s so horribly roundabout that it’s useless as an explanation of closures.

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

Sidebar

Ask A Question

Stats

  • Questions 517k
  • Answers 517k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer AES, the Advanced Encryption Standard, defines in FIPS PUB 197… May 16, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer For C/C++ I would recommend the GNU Multiple Precision Library.… May 16, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer Yes, you may checkout this article illustrating how to render… May 16, 2026 at 7:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I started learning Python using the IronPython implementation. I'd like to do some web
I was learning python using the tutorial that comes with the standard python installation.
I'm currently learning Python using Zelle's Introductory text, and I'm trying to recreate one
I've recently been learning python and I just started playing with networking using python's
I have started learning Python by writing a small application using Python 3.1 and
I am about to embark on learning Python (largely for the purposes of using
I'm learning Python (and it's my first programming language so don't be too intense
I currently use Python for most of my programming, but I'm interested in learning
Hello Stack Overflow contributers, I'm a novice programmer learning Python right now, and I
I am trying improve my programming skills reading other peoples code, but I'd like

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.