object.enable() anti-pattern

Nobody nobody at nowhere.com
Fri May 10 12:59:26 EDT 2013


On Thu, 09 May 2013 05:23:59 +0000, Steven D'Aprano wrote:

> There is no sensible use-case for creating a file without opening it. 
> What would be the point? Any subsequent calls to just about any method 
> will fail. Since you have to open the file after creating the file object 
> anyway, why make them two different calls?

As a counterpoint, some OSes (e.g. Plan 9) allow you to get a "handle" to
a file without opening it. This can then be used for deleting, renaming or
stat()-type operations without either the risk of race conditions (if
another process renames files between operations, the operations may
be performed on different files) or the side-effects of actually opening
the file (particularly for device files, e.g. opening a tape drive may
rewind the tape).

Python's file model doesn't allow for this, so there isn't really anything
meaningful that you can do on a file object which isn't open (although
these actually exist; any file object on which the .close() method has
been called will be in this state).

However: there are situations where it is useful to be able to separate
the simple task of creating an object from more invasive actions such as
system calls. Particularly in multi-threaded or real-time code (although
the latter is a non-starter in Python for many other reasons).




More information about the Python-list mailing list