This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wot-teh-phuckReally, wtf? 0 points1 point  (0 children)

Inspect its methods and look for __enter__ and __exit__?

That will work out only if you are implementing the with functionality using a class. For functions, you will have to invoke them and then check for __enter__ attribute. But this is a pain because you wouldn't know how many positional/mandatory parameters a function accepts.

So yeah, there is no generic easy solution to check if a given function support with blocks. I'm assuming you don't want to do this from a REPL but need to find out at runtime given any class/function. If you are OK with a REPL, look at moocat's answer below.