[Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')

Alan Gauld alan.gauld at btinternet.com
Tue May 5 01:41:39 CEST 2009

"Emile van Sebille" <emile at fenx.com> wrote in message 
news:gtnrtf$pi8$1 at ger.gmane.org...
> On 5/4/2009 2:50 PM bob gailer said...
>> PDavid wrote:
>>> Dear list,
>>> in different books I come across different syntax for dealing with
>>> files. It seems that open(filename, 'r') and file(filename, 'r') are
>>> used interchangeably, and I wonder what this is all about. Is there a
>>> reason why Python allows such ambiguity here?
> Backwards compatibility.  The file type was introduced in python 2.2, 
> before which there was open.

And file has been removed again in Python v3....
In fact open is now an alias for io.open and no longer simply returns
a file object - in fact the file type itself is gone too!

A pity, there are cases where I found file() more intuitive than
open and vice versa so liked having both available. The fact that it
looked like creating an instance of a class seemed to fit well
in OO code.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list