overriding file.readline: "an integer is required"

Miles semanticist at gmail.com
Wed Jul 30 22:47:04 EDT 2008


On Wed, Jul 30, 2008 at 5:52 PM, kj <socyl at 987jk.com.invalid> wrote:
> I know that I could rewrite the method like this:
>
>    def readline(self, size=None):
>        if size == None:
>            line = self.file.readline()
>        else:
>            line = self.file.readline(size)
>        # etc., etc.
>
> ...but this seems to me exceptionally awkward.  (Actually, it's worse
> than awkward: it fails to complain when the overriding method is
> called with the argument None.

IMO, the method should have been possible to call with None in the
first place (like str.split and list.sort), and it's not incorrect for
your method to allow it.

> It would be better to test for the
> number of arguments passed to the function, but I can't figure out
> how to do this either.)

You could of course do:

    def readline(self, *args):
        line = self.file.readline(*args)
        # etc., etc.

But this has its drawbacks.

-Miles



More information about the Python-list mailing list