overriding file.readline: "an integer is required"

K

kj

I'm trying to subclass file, overriding the readline method. The
new method definition begins with

def readline(self, size=None):
line = self.file.readline(size)
# etc., etc.

....where the self.file attribute is a regular file object.

This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error

TypeError: an integer is required

....which I suppose comes from the call to self.file.readline(None).

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. 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.)

Is there a better idiom?

TIA!

Kynn
 
G

Gary Herron

kj said:
I'm trying to subclass file, overriding the readline method. The
new method definition begins with

def readline(self, size=None):
line = self.file.readline(size)
# etc., etc.

...where the self.file attribute is a regular file object.

This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error

TypeError: an integer is required

...which I suppose comes from the call to self.file.readline(None).

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. 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.)

Is there a better idiom?

Since the manual implies that negative values have no effect, try this:

def readline(self, size=-1):
line = self.file.readline(size)
# etc., etc.

I tested this (just barely), and it seems to work as you wish.

Gary Herron
 
M

Miles

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
 
G

Gabriel Genellina

In <[email protected]> Miles





Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)

One thing I can think of: The help system (and the code autocompleter
found in some editors, and other introspection tools) can't tell you the
name/number/default value of the arguments anymore: they're all masked
into a generic *args
 
T

Terry Reedy

Not obvious from the docs, but readline(-1) should be the same as
readline().
(In 3.0b2, None also works, but this is not yet documented.) So try:

def readline(self, size=-1):
line = self.file.readline(size)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top