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

wesley chun wescpy at gmail.com
Tue May 5 20:38:03 CEST 2009


> It seems that open(filename, 'r') and file(filename, 'r') are
> used interchangeably, and I wonder what this is all about.


as alan and others have pointed out, file() was added in 2.2. this was
part of the unification of types and classes. every object now has a
factory function, i.e., list() creates a list, and same goes for int,
float, dict, etc., and of course, file() creates a file.

open(), on the other hand, has been around for ages and will remain so
while file() is removed in 3.0 -- yes, easy come, easy go.  the
original intention was *not* to have file() for opening files with,
but rather for type checking.  in other words:

OPEN A FILE with open(), i.e., f = open('foo.txt', 'r')
TEST FOR A FILE with file(), i.e., if isinstance(f, file): ...

does anyone have the idiom for the above isinstance() check for 3.x?

hope the rest of this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list