On Wed, 08 May 2013 08:52:12 +0000, Steven D'Aprano wrote: > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: > > f = file("some_file.txt") > f.open() > data = f.read()
So why don't we do this? data = read("some_file.txt") > because reading the file can fail if you forget to call open first. > Instead, Python uses a factory function that creates the file object and > opens it: > > f = open("some_file.txt") # if this succeeds, f is ready to use > data = f.read() That's just the "enable" paradigm in a socially acceptable and traditional wrapper. Opening and closing the file is an implementation detail, often defeated by caching (application level or OS level) anyway. Dan -- http://mail.python.org/mailman/listinfo/python-list