Awful code of the week

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Aug 9 04:12:59 EDT 2016


On Tuesday 09 August 2016 15:37, Chris Angelico wrote:

[...]
>> You can't be too careful with memory management.
> 
> Right. Of course, it gets very onerous, so we tend to use a context
> manager instead.
> 
>     def process(self, stuff):
>         with deallocate() as cleanup:
>             cleanup(self)
>             cleanup(stuff)
>             files = self.files
>             cleanup(files)
>             del files
>             files = []
>             cleanup(files)
>             files = self.files
>             cleanup(files)
>             for file in files:
>                 cleanup(file)
>                 file.process(stuff)
>             return 1
> 
> There, isn't that so much better?

Ha! That's why I never use context managers -- they're too easy to write buggy 
code. You forgot to deallocate cleanup itself! Memory leak!



-- 
Steve




More information about the Python-list mailing list