By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

overriding file.readline: "an integer is required"

P: n/a
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
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jul 30 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
kj wrote:
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

TIA!

Kynn
Jul 30 '08 #2

P: n/a
On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
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
Jul 31 '08 #3

P: n/a
kj
In <ma************************************@python.org Miles <se*********@gmail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
>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.
>You could of course do:
def readline(self, *args):
line = self.file.readline(*args)
# etc., etc.
>But this has its drawbacks.
Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)

TIA!

kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Aug 1 '08 #4

P: n/a
En Fri, 01 Aug 2008 16:21:26 -0300, kj <so***@987jk.com.invalidescribi�:
In <ma************************************@python.org Miles
<se*********@gmail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
>>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.
>You could of course do:
> def readline(self, *args):
line = self.file.readline(*args)
# etc., etc.
>But this has its drawbacks.

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

--
Gabriel Genellina

Aug 5 '08 #5

P: n/a


Gabriel Genellina wrote:
>>> def readline(self, size=None):
if size == None:
line = self.file.readline()
else:
line = self.file.readline(size)
# etc., etc.
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)

Aug 5 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.