Proposed extension to file constructor
oren-py-l at hishome.net
Wed Jun 4 14:36:09 CEST 2003
On Tue, Jun 03, 2003 at 02:40:28PM -0500, Skip Montanaro wrote:
> taral> I needed to extend popen functionality, so I modified the file
> taral> constructor to optionally take a file descriptor. It was, in my
> taral> opinion, the minimum way to do it while preserving isinstance(x,
> taral> file).
> Where are you getting file descriptors? What's wrong with using os.fdopen
> to wrap a file descriptor in a file object?
Since 2.2 the file object is a valid base class for deriving subclasses but
if they override the __init__ method they can't set the file descriptor field
except by calling file.__init__ which open()s a string path. If you already
have a file descriptor you can call fdopen but then the result can't be a
subclass of the built-in file type.
One example of why you might want a file subclass is to extend the functionality
of the object returned by popen with new methods (e.g. retrieving process the
exit code). Why should adding a single method require the implementation of a
complete wrapper object? Another functionality that is available only for a true
file object and not for subclasses is to set the name field to an arbitrary
value such as '(fdopen)' or a process name.
More information about the Python-list