For example:
class Foo:
def __init__(self):
self.bar = "baz"
def test(self):
return "secret value that can only accessed in this function!!"
How can I do this:
x = Foo()
with x.test() as f:
print(f)
# "secret value that can only be accessed in this function!!"
Without raising an error?
withis used for contextmanagers, not all methods will return a context manager, have a look at stackoverflow.com/questions/3012488/…Foo.testan instance method if it doesn't use the instance? And ifFoodoesn't really do anything, why not maketesta plain function? Why do you need to use awithstatement instead of just calling the method?