Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Context Manager (with)
#1
So a Context Manager with statement class must have __enter__() and __exit__() .

For example file objects with will automatically close the file.

with open("/Users/user/tmp/workData.txt", "w") as work_data:
    for value in h:
        str_value = str(value)
        work_data.write(str_value)
        work_data.write("\n")
#1) How does one know if a class object supports the Context Manager. What should I be looking for in the docs?

#2) How do you overwrite it (as in file objects for example?)

Thanks
Reply


Messages In This Thread
Context Manager (with) - by wyattbiker - Jul-22-2018, 08:08 PM
RE: Context Manager (with) - by Larz60+ - Jul-22-2018, 09:00 PM
RE: Context Manager (with) - by buran - Jul-23-2018, 04:32 AM
RE: Context Manager (with) - by snippsat - Jul-23-2018, 03:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Context-sensitive delimiter ZZTurn 9 1,516 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,354 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,048 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  How to create an app manager _ShevaKadu 8 3,814 Nov-01-2020, 12:47 PM
Last Post: _ShevaKadu
  TextIOWrapper.tell() with Python 3.6.9 in context of 0D/0A fschaef 0 2,086 Mar-29-2020, 09:18 AM
Last Post: fschaef
  Is it OK to use a context manager to simplify attribute access? nholtz 0 2,053 Jun-11-2019, 01:19 AM
Last Post: nholtz
  Smtplib: What does context argument means? Pythenx 1 3,093 Mar-27-2019, 06:25 PM
Last Post: nilamo
  Dealing with multiple context managers heras 5 4,727 Nov-16-2018, 09:01 AM
Last Post: DeaD_EyE
  Trouble with a context manager class made with dunders Regulus 1 2,677 Jan-28-2018, 03:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020