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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:50:21+00:00 2026-06-11T19:50:21+00:00

I’ve been playing around with making my own context managers in Python. I’m seeing

  • 0

I’ve been playing around with making my own context managers in Python. I’m seeing some strange behavior most likely due to my implementation.

I see the __exit__ code called before a statement in the ‘with’ context. For example, here is the code snippet:

with ProgressBar(10) as p:
  p.update(1)

and it’s exception:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'update'

I put debug in all the __enter__, __exit__, and update methods of my context manager. It looks like __exit__ is called before update(). This makes no sense so I must be missing something simple.

Here is my simple context manager class:

class ProgressBar(object):
    """Progress bar that normalizes progress to [0 - 100] scale"""

    def __init__(self, max_value):
        """Create progress bar with max_value"""

        self._current_value = 0.0
        self._completed_value = 100.0
        self._max_value = float(max_value)
        print 'init', max_value

    def __enter__(self):
        """Start of context manager, 'with' statement"""

        print 'enter'
        self._current_value = 0.0

    def __exit__(self, exc_type, exc_value, traceback):
        """Start of context manager, 'with' statement"""

        print 'exit'
        self._current_value = self._completed_value

        # Not handling any exceptions, so they'll be raised automatically
        # To ignore exceptions return True or inspect arguments to handle

        return False

    def update(self, value):
        """Update progress value"""

        print 'update'
        if value >= self._max_value:
            self._current_value = 100
        else:
            self._current_value = (value / self._max_value) * self._completed_value

        print '\r%s' % (self._current_value),
  • 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-06-11T19:50:23+00:00Added an answer on June 11, 2026 at 7:50 pm

    From the documentation:

    object.__enter__(self)

    Enter the runtime context related to this object. The with statement
    will bind this method’s return value to the target(s) specified in
    the as clause of the statement, if any.

    You’re not returning anything from __enter__ (and thus you’re returning None, as always). If you return self, you’ll get

    init 10
    enter
    update
    10.0 exit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.