r/Python • • Nov 25 '22

Intermediate Showcase defer in python!

https://github.com/dankeyy/defer.py

stupid but works lol hope you like it

298 Upvotes

62 comments sorted by

View all comments

Show parent comment

4

u/kezmicdust Nov 25 '22

Couldn’t you just write in the close statement and then just write the “stuff you want to do with f” between the open and close statements?

Or am I missing something?

12

u/relvae Nov 25 '22

If an exception is thrown then close wouldn't be called in that case. Python already has context managers to deal with that but OP has gone rogue lol

1

u/kezmicdust Nov 25 '22

I see! Thanks.

2

u/antiproton Nov 25 '22

It's just a different way to solve similar problems. Similar to putting the close in a try...except...finally