On Friday, March 2, 2018 at 12:14:53 AM UTC, Chris Angelico wrote:
> On Fri, Mar 2, 2018 at 11:07 AM,  ooomzay wrote:
> > On Thursday, March 1, 2018 at 11:59:26 PM UTC, Chris Angelico wrote:
> >> On Fri, Mar 2, 2018 at 10:38 AM,  ooomzay wrote:
> >> > def raii_example():
> >> >
> >> >     src = RAIIFileAccess("src.txt", 'r')
> >> >     dst = RAIIFileAccess("dst.txt", 'w')
> >> >
> >> >     for line in src:
> >> >         dst.write(line)
> >>
> >> What happens if we make this change?
> >>
> >> def raii_example():
> >>     global dst
> >>     src = RAIIFileAccess("src.txt", 'r')
> >>     dst = RAIIFileAccess("dst.txt", 'w')
> >>
> >>     for line in src:
> >>         dst.write(line)
> >>
> >> When does the destination file get closed?
> >
> > When you execute:-
> >
> >    del dst
> >
> > or:-
> >
> >    dst = something_else
> 
> What if you don't?

Then the resource will remain open until your script exits at which point it is 
probably not very well defined exactly when or even if the destructor/__del__ 
will be called.

I.e. Don't do this! Did you have some realistic case in mind or are you just 
probing the behaviour?


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to