by jlarocco · 14y ago · 0 comments
with open("text.txt") as inf:
print(inf.read())
User defined classes that implement special methods __enter__() and __exit__() can be used as context managers. More info: http://www.python.org/dev/peps/pep-0343/For just file output/input Scheme has the "with-output-to-file" and "with-input-from-file" macros which redirect output and input to a given file. I vaguely remember Common Lisp having a similar thing, but I can't remember the name off hand.
http://www.lispworks.com/documentation/HyperSpec/Body/m_w_op...
for line in open("name.txt"):
print line # Or do something else with it.No, it's a code bug.
> Once the iterator finishes, the file should be closed and the file handler freed.
There is no reason for that to happen, let alone for that to happen deterministically.
def in_context(*args) setup_context yield if block_given? ensure teardown_context end
I guess you could build a special class, but why?