Jul-23-2018, 03:19 PM
(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'