by glifchits · 8y ago · 0 comments
x = open('filename')
x.closed # False
with x:
print(x.readline())
x.closed # True
I think you're right. I prefer the `as` variant for readability. def __enter__(self):
return self
which is a common pattern (used by open()), but there's no requirement that __enter__() return the same object.In cases where __enter__() does something different, the assignment expression and the 'as' variable would have different values (the object of the 'with' statement, x, and the result of calling x.__enter__(), respectively).