Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!panix!panix2.panix.com!not-for-mail From: roy@panix.com (Roy Smith) Newsgroups: comp.lang.python Subject: Re: object.enable() anti-pattern Date: 9 May 2013 14:59:26 -0400 Organization: PANIX -- Public Access Networks Corp. Lines: 39 Message-ID: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1368125966 14002 166.84.1.2 (9 May 2013 18:59:26 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 9 May 2013 18:59:26 +0000 (UTC) Xref: csiph.com comp.lang.python:45054 In article <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com>, Steven D'Aprano wrote: > There is no sensible use-case for creating a file OBJECT unless it > initially wraps an open file pointer. OK, I guess that's a fair statement. But mostly because a python file object only exposes those subset of operations you can do on file descriptors which deal with reading and writing the contents of a file. It would not be true if python file objects included methods for querying and manipulating file metadata. It's not hard to imagine a file class which could be used like: f = file("/path/to/my/file") f.delete() That would be a totally different model from the current python file object. And then there would be plenty of things you might want to do to a file other than open it... file("/path/to/my/directory").chdir() file("/dev/sdf").mount("/var/lib/whatever") file("/mnt/swapfile").swapon() > The standard C I/O library doesn't support creating a file > descriptor unless it is a file descriptor to an open file [...] > there is no corresponding function to create a *closed* file > description. (Because such a thing would be pointless.) What about sockets? From the python standard library: s = socket.socket() Now what? You can't do much with your shiny new socket until you call bind() or connect(), or a few other things. At least not for TCP. This is essentially the two-phased construction pattern. Of course, the python socket module just exposes the semantics of the underlying OS sockets, so there's not a lot of choice there.