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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:12:36+00:00 2026-06-11T23:12:36+00:00

I came across this as a bit of a surprise while trying to work

  • 0

I came across this as a bit of a surprise while trying to work out another question.

This seemed extremely odd to me, I thought it was worth asking the question. Why doesn’t __getattr__ appear to work with with?

if I make this object:

class FileHolder(object):
    def __init__(self,*args,**kwargs):
        self.f= file(*args,**kwargs)

    def __getattr__(self,item):
        return getattr(self.f,item)

and using it with with,

>>> a= FileHolder("a","w")
>>> a.write
<built-in method write of file object at 0x018D75F8>
>>> with a as f:
...   print f
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>> a.__exit__
<built-in method __exit__ of file object at 0x018D75F8>

Why does this happen?

EDIT

>>> object.__exit__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'object' has no attribute '__exit__'

It definitely isn’t inheriting __exit__

  • 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-11T23:12:37+00:00Added an answer on June 11, 2026 at 11:12 pm

    I can’t say for sure, but after reading over the PEP describing the with statement:

    http://www.python.org/dev/peps/pep-0343/

    This jumped out at me:

    A new statement is proposed with the syntax:
    
        with EXPR as VAR:
            BLOCK
    
    ....
    
    The translation of the above statement is:
    
        mgr = (EXPR)
        exit = type(mgr).__exit__  # Not calling it yet
        value = type(mgr).__enter__(mgr)
    
    ....
    

    Right there. The with statement does not call __getattr__(__exit__) but calls type(a).__exit__ which does not exist giving the error.

    So you just need to define those:

    class FileHolder(object):                                                                                                                 
        def __init__(self,*args,**kwargs):
            self.f= file(*args,**kwargs)
    
        def __enter__(self,*args,**kwargs):
            return self.f.__enter__(*args,**kwargs)
    
        def __exit__(self,*args,**kwargs):
            self.f.__exit__(*args,**kwargs)
    
        def __getattr__(self,item):
            return getattr(self.f,item)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this bit of vba code posted in another SO question. Is
I thought I understood variable scope until I came across this bit of code:
I'm trying to do a bit of charting and came across this weird error
Hi I came across this bit of C++ code and am trying to learn
A bit of a neophyte haskell question, but I came across this example in
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I was doing a bit research and I came across this comment. I have
I came across this situation while migrating our DB from Foxpro to SQL. Below
I came across this problem at work, and though I have a solution, I
I'm writing a programming language, and when I came across this question , my

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.