why is open() a builtin function? 
Author Message
 why is open() a builtin function?

Shouldn't the "file" object class have an __init__ method, instead?

Thus, instead of writing

        foo = open("fname", "rb+")

I'd write

        foo = file("fname", "rb+")

This makes it easy to create new special file types:

        class bitstream(file):
                ...

As it is, the best I can do is say

        class bitstream:
                def __init__(fileobj):
                        ...
        # end of class bitstream

        foofile = open("fname", "rb+")
        foobit = bitstream(foofile)

- David



Sun, 01 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?

Quote:
>Shouldn't the "file" object class have an __init__ method, instead?

>Thus, instead of writing

>    foo = open("fname", "rb+")

>I'd write

>    foo = file("fname", "rb+")

>This makes it easy to create new special file types:

>    class bitstream(file):
>            ...

>As it is, the best I can do is say

>    class bitstream:
>            def __init__(fileobj):
>                    ...
>    # end of class bitstream

>    foofile = open("fname", "rb+")
>    foobit = bitstream(foofile)

>- David

I'm sort of improvising here, so bear with me...

It might be a good idea not to mix the concept of a bitstream and the concept
of a file. With the second approach the bitstream can use any object supporting
the interface you require. Look at the pickle module for instance. By
separating the concerns (serializing objects and storing them) the module is
very flexible (I use pickle to implement remote calls.)

Just some thoughts,
--
------------------------------------------------------------------

ABB Industrial Systems AB       phone: +46-21-34 30 29
S-721 67 Vaesteraas, Sweden     fax  : +46-21-34 25 55



Mon, 02 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?

Quote:
>Shouldn't the "file" object class have an __init__ method, instead?

>Thus, instead of writing

>    foo = open("fname", "rb+")

>I'd write

>    foo = file("fname", "rb+")

>This makes it easy to create new special file types:

>    class bitstream(file):
>            ...

>As it is, the best I can do is say

>    class bitstream:
>            def __init__(fileobj):
>                    ...
>    # end of class bitstream

>    foofile = open("fname", "rb+")
>    foobit = bitstream(foofile)

>- David

I'm sort of improvising here, so bear with me...

It might be a good idea not to mix the concept of a bitstream and the concept
of a file. With the second approach the bitstream can use any object supporting
the interface you require. Look at the pickle module for instance. By
separating the concerns (serializing objects and storing them) the module is
very flexible (I use pickle to implement remote calls.)

Just some thoughts,
--
------------------------------------------------------------------

ABB Industrial Systems AB       phone: +46-21-34 30 29
S-721 67 Vaesteraas, Sweden     fax  : +46-21-34 25 55



Mon, 02 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?


Quote:
>>As it is, the best I can do is say

>>        foofile = open("fname", "rb+")
>>        foobit = bitstream(foofile)

>It might be a good idea not to mix the concept of a bitstream and the concept
>of a file. With the second approach the bitstream can use any object supporting
>the interface you require. Look at the pickle module for instance. By
>separating the concerns (serializing objects and storing them) the module is
>very flexible (I use pickle to implement remote calls.)

This is a very good point.  Perhaps we want to more clearly
separate the idea of "files" and "streams" as in Lisp.
There, streams are sources or sinks of data, and there are a
collection of routines which will give you a stream where the
back end is a file, string, ....

This would push the open() builtin into the background, since
we'd say

        filestream = stream.file("pathname")
        stringstream = stream.string("a long string")

"stream" would have to be a module which defined the stream
class and provided constructor functions for all the different
types of sinks/sources which did magic.

All stream objects need to support are read, write, flush,
probably type() (which would return a file or string type
object of some sort).

- David



Mon, 02 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?
Quote:
> As it is, the best I can do is say

>    class bitstream:
>            def __init__(fileobj):
>                    ...
>    # end of class bitstream

>    foofile = open("fname", "rb+")
>    foobit = bitstream(foofile)

Not to mention:
        def write(self,data): self.foofile.write(data)
        def read(..
        def flush(...
etc.

I must admit I have wondered that myself.  A file seems to be about
the only object for which a built-in function _must_ be used to
create one.

Mark.
------------
Mark Hammond - Skippi-Net, Melbourne, Australia.
I can speak for Skippi-Net, 'cos I'm it!



Tue, 03 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?

    MH> I must admit I have wondered that myself.  A file seems to be
    MH> about the only object for which a built-in function _must_ be
    MH> used to create one.

I have a related complaint about the string module.  It seems
backwards to me to have to do:

    spam='spam and eggs'
    words = string.split(spam)

when the last line should (IMHO) be

    words = spam.split()

But I understand the historical nature of strings not being class
instances.

-Barry



Tue, 03 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?

Quote:
> I have a related complaint about the string module.  It seems
> backwards to me to have to do:

>     spam='spam and eggs'
>     words = string.split(spam)

> when the last line should (IMHO) be

>     words = spam.split()

> But I understand the historical nature of strings not being class
> instances.

It has little to do with strings being class instances -- built-in
objects could also have such operations as methods.

It has to do with history, though -- when designing the string data
type, I didn't know what other operations on strings (besides
indexing, slicing and concatenation) would be important enough to
warrant making them built-in operations on strings.

Having string operations, built-in or otherwise, live in separate
modules means that the string object implementation doesn't need to be
updated each time someone comes up with new functionality.
(E.g. while string.lower() and string.split() seem to warrant being
methods of the string datatype itself, the case is less clear-cut for
regular expression operations.  And what about image and audio data
operations (all of which use strings as datatypes)?

Clearly, one has to draw a line.  Perhaps I drew the line a little to
closely to the string implementation, but it's not a sufficiently
major problem that I'd like to change it.


URL: <http://www.python.org/~guido/>



Tue, 03 Mar 1998 03:00:00 GMT  
 why is open() a builtin function?




[...]

    David>   filestream = stream.file("pathname") stringstream =
    David> stream.string("a long string")

Or even

stream-object = stream.stream(open("filename"))

Why have predetermined stream types? As long as the object supports
some member set it could form the basis of a stream. Then I could add
a new stream type from "outside" the base objects ( as in C++
iostreams ).

    David> "stream" would have to be a module which defined the stream
    David> class and provided constructor functions for all the
    David> different types of sinks/sources which did magic.

    David> All stream objects need to support are read, write, flush,
    David> probably type() (which would return a file or string type
    David> object of some sort).

    David> - David
--
----------------------------------------------------------------------------
Chris Vale                       | "Freedom and not servitude is the cure of

                                 |  the true remedy of superstition."
                                 |                      Edmund Burke
----------------------------------------------------------------------------



Tue, 03 Mar 1998 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Builtin methods versus Builtin Functions -- help!

2. Pickler that handles modules, functions, and builtin functions -- done

3. generic functions to redefine builtin functions - how ?

4. why winrun api function does open iexplorer

5. __builtin__.open on windows NT

6. DECIMAL Builtin Function

7. Regina vs IBM REXX: builtin functions

8. How to get the Callable of builtin functions

9. builtin functions

10. i need help on builtin function math

11. filterout() builtin function

12. Name od builtin functions: is there a rule ??

 

 
Powered by phpBB® Forum Software