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
#2
you might want to look at the examples here:
to determine if something uses context manager, you can use:
assuming method in class:
def uses_context_manager(self, mymethod)
    istrue = False
    istrue = hasattr(mymethod, '__exit__'):
    return istrue
Reply
#3
check also contextlib - https://docs.python.org/3/library/contextlib.html
with @conextlib.contetxmanager decorator you can define a factory function for with statement context managers, without needing to create a class or separate __enter__() and __exit__() methods.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jul-22-2018, 08:08 PM)wyattbiker Wrote: #1) How does one know if a class object supports the Context Manager. What should I be looking for in the docs?
I supporter pretty much every everywhere as it's just syntax sugar,
and with open() for files was added 12-year ago in Python 2.5.
Quote:#2) How do you overwrite it (as in file objects for example?)
Not sure if i know you mean,but not much not difference,it's the io that is in bottom which control all.
# The new way(added 12-year ago)
with open('foo.txt', 'w', encoding='utf-8') as f:
    pass

# Old way
other_f = open('bar.txt', 'w', encoding='utf-8')
Test it:
>>> f
<_io.TextIOWrapper name='foo.txt' mode='w' encoding='utf-8'>
>>> other_f
<_io.TextIOWrapper name='bar.txt' mode='w' encoding='utf-8'>

>>> type(f)
<class '_io.TextIOWrapper'>
>>> type(other_f)
<class '_io.TextIOWrapper'>

>>> f = 'overwrite'
>>> other_f = 'me to'
>>> f
'overwrite'
>>> other_f
'me to'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Context-sensitive delimiter ZZTurn 9 1,392 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,263 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,011 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  How to create an app manager _ShevaKadu 8 3,695 Nov-01-2020, 12:47 PM
Last Post: _ShevaKadu
  TextIOWrapper.tell() with Python 3.6.9 in context of 0D/0A fschaef 0 2,040 Mar-29-2020, 09:18 AM
Last Post: fschaef
  Is it OK to use a context manager to simplify attribute access? nholtz 0 2,022 Jun-11-2019, 01:19 AM
Last Post: nholtz
  Smtplib: What does context argument means? Pythenx 1 3,049 Mar-27-2019, 06:25 PM
Last Post: nilamo
  Dealing with multiple context managers heras 5 4,600 Nov-16-2018, 09:01 AM
Last Post: DeaD_EyE
  Trouble with a context manager class made with dunders Regulus 1 2,642 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