Shouldn't the return value for the __enter__
method be self
always.
Python documentation says :
object.__enter__(self)
Enter the runtime context related to this object. The with statement will bind this method’s return value to the target(s) specified in the as clause of the statement, if any.
With this, to do anything practical, should't self
be returned always from __enter__
method of a class as without it one would not be able to call other class methods on the context.
For example, in the following code, s.main() works fine but b1.main() errors out.
class a(object):
def __init__(self):
pass
def __enter__(self):
return self
def __exit__(self ,type, value, traceback):
return self
def main(self):
print " in a::main self %d " , id(self)
class b(object):
def __init__(self):
pass
def __enter__(self):
return "something else"
def __exit__(self ,type, value, traceback):
pass
def main(self):
print "in b::main !! self id " , id(self)
with a() as s:
s.main()
with b() as b1:
b1.main()
s = a()
s.main()
self
from__enter__
". And, well, that depends on your fantasy. – Wilinesstempfile.TemporaryDirectory
in the standard library. – Antares