[Tutor] deriving class from file to handle input line numbers?

Duncan Gibson duncan at thermal.esa.int
Tue Aug 16 15:35:52 CEST 2005


I wrote:
> >     class MyFile(file):
> >         etc
> > 
> > I couldn't see how to have an instance of MyFile returned from the
> > built-in 'open' function. I thought this was the crux of the problem.

Kent Johnson replied:
> open() is actually just an alias for file():
>  >>> open is file
> True

Thank you very much! You have just provided me with the vital piece of
information I needed and everything has just clicked into place.

Now that I know that I've searched the documentation again and found:

    The file() constructor is new in Python 2.2 and is an alias for
    open(). Both spellings are equivalent. The intent is for open()
    to continue to be preferred for use as a factory function which
    returns a new file object. The spelling, file is more suited to
    type testing (for example, writing "isinstance(f, file)").

See http://docs.python.org/lib/built-in-funcs.html#l2h-25

Cheers
Duncan


More information about the Tutor mailing list