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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:44:09+00:00 2026-05-18T03:44:09+00:00

I would like to use with on an object that uses __getattr__ to redirect

  • 0

I would like to use with on an object that uses __getattr__ to redirect calls.
Howerver, this does not seem to work with the method __enter__

Please consider the following, simplified code to reproduce the error:

class EnterTest(object):
    def myenter(self):
        pass
    def __exit__(self, type, value, traceback):
        pass

    def __getattr__(self, name):
        if name == '__enter__':
            return self.myenter

enter_obj = EnterTest()
print getattr(enter_obj, '__enter__')

with enter_obj:
    pass

Output:

<bound method EnterTest.myenter of <__main__.EnterTest object at 0x00000000021432E8>>
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    with enter_obj:
AttributeError: __enter__

Why doesn’t it fall back to __getattr__ since __enter__ does not exist on the object?

Of course, I could make it work if I just create an __enter__ method and redirect from there instead, but I’m wondering why it doesn’t work otherwise.

My python version is the following:

C:\Python27\python27.exe 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]
  • 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-18T03:44:10+00:00Added an answer on May 18, 2026 at 3:44 am

    According to upstream, this working was a bug in 2.6 which was “fixed” in 2.7. The short answer is that methods like __enter__ are looked up on the class, not on the object.

    The documentation for this obscure behavior is at http://docs.python.org/reference/datamodel#specialnames: x[i] is roughly equivalent to ... type(x).__getitem__(x, i) for new-style classes.

    You can see this behavior with other special methods:

    class foo(object):
        def __iadd__(self, i):
            print i
    a = foo()
    a += 1
    
    class foo2(object):
        def __getattr__(self, key):
            print key
            raise AttributeError
    b = foo2()
    b += 1
    
    class foo3(object):
        pass
    def func(self, i):
        print i
    c = foo3()
    c.__iadd__ = func
    c += 1
    

    The first works; the second two don’t. Python 2.6 didn’t conform to this behavior for __enter__ and __exit__, but 2.7 does. http://bugs.python.org/issue9259

    That said, it’s painfully inconsistent that these methods can’t be handled dynamically like any other attributes can. Similarly, you can’t instrument accesses to these methods with __getattribute__ like you can any other method. I can’t find any intrinsic design logic to this. Python is normally very consistent, and this is a fairly unpleasant wart.

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

Sidebar

Related Questions

I would like to create/use a system-wide independent universal 'counter object' that can be
I work on a project which uses Spring Security. I would like to use
I would like to use XHR object's progress event to get the progress of
I would like to use a COM object in my application. How can I
I would like to create one EMF object and use it in various Resource
I use JSON.NET and I would like to parse the following object which I
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to be able to make an application that uses open layers
There is this DB-connection-like object that my web application will need. It is pretty

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.