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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:19:32+00:00 2026-05-26T16:19:32+00:00

The standard library open function works both as a function: f = open(‘file.txt’) print(type(f))

  • 0

The standard library open function works both as a function:

f = open('file.txt')
print(type(f))
<type 'file'>

or as a context manager:

with open('file.txt') as f:
    print(type(f))
<type 'file'>

I am trying to mimic this behaviour using contextlib.closing, where File is my custom file I/O class:

def my_open(filename):
    f = File(filename)
    f.open()
    return closing(f)

this works as expected as a context manager:

with my_open('file.txt') as f:
    print(type(f))
<class '__main__.File'>

but of course if I call directly, I get back the closing object instead of my object:

f = my_open(filename)
print(type(f))
<class 'contextlib.closing'>

So, how do I implement my_open so that it both works as a context manager and returns my File object when called directly?

Full working example on github:
https://gist.github.com/1352573

  • 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-26T16:19:32+00:00Added an answer on May 26, 2026 at 4:19 pm

    The easiest thing is probably to implement the __enter__ and __exit__ methods yourself. Something like this should do it:

    class File(object):
       # ... all the methods you already have ...
    
       # context management
       def __enter__(self):
           return self
    
       def __exit__(self, *exc_info):
           self.close()
    

    It would, by the way, be more idiomatic to do the work of your open method in your __init__ method.

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

Sidebar

Related Questions

Why is calling a standard library function inside a signal handler discouraged?
The strchr function in the C standard library looks for a char in a
I depend heavily on Python's standard library , both for useful data structures and
I'm trying to use the FreeImage.Net library to open the image and convert it
In standard library, I found that namespace std is declared as a macro. #define
Is there a standard library method that converts a string that has duration in
Is WPF an ECMA standard library? Or it is just a part of MS
Is there a class in the standard library of .NET that gives me the
I've always wondered why the C++ Standard library has instantiated basic_[io]stream and all its
If for example I should not use standard library functions like printf() , putchar()

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.