overriding file.readline: "an integer is required"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kj

    overriding file.readline: "an integer is required"



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

    def readline(self, size=None):
    line = self.file.readl ine(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.readl ine(None).

    I know that I could rewrite the method like this:

    def readline(self, size=None):
    if size == None:
    line = self.file.readl ine()
    else:
    line = self.file.readl ine(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.
  • Gary Herron

    #2
    Re: overriding file.readline: "an integer is required"

    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.readl ine(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.readl ine(None).
    >
    I know that I could rewrite the method like this:
    >
    def readline(self, size=None):
    if size == None:
    line = self.file.readl ine()
    else:
    line = self.file.readl ine(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.readl ine(size)
    # etc., etc.

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

    Gary Herron



    TIA!
    >
    Kynn
    >

    Comment

    • Miles

      #3
      Re: overriding file.readline: "an integer is required"

      On Wed, Jul 30, 2008 at 5:52 PM, kj <socyl@987jk.co m.invalidwrote:
      I know that I could rewrite the method like this:
      >
      def readline(self, size=None):
      if size == None:
      line = self.file.readl ine()
      else:
      line = self.file.readl ine(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.readl ine(*args)
      # etc., etc.

      But this has its drawbacks.

      -Miles

      Comment

      • kj

        #4
        Re: overriding file.readline: &quot;an integer is required&quot;

        In <mailman.953.12 17472439.922.py thon-list@python.org Miles <semanticist@gm ail.comwrites:
        >On Wed, Jul 30, 2008 at 5:52 PM, kj <socyl@987jk.co m.invalidwrote:
        >I know that I could rewrite the method like this:
        >>
        > def readline(self, size=None):
        > if size == None:
        > line = self.file.readl ine()
        > else:
        > line = self.file.readl ine(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.readl ine(*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.

        Comment

        • Gabriel Genellina

          #5
          Re: overriding file.readline: &quot;an integer is required&quot;

          En Fri, 01 Aug 2008 16:21:26 -0300, kj <socyl@987jk.co m.invalidescrib i�:
          In <mailman.953.12 17472439.922.py thon-list@python.org Miles
          <semanticist@gm ail.comwrites:
          >
          >On Wed, Jul 30, 2008 at 5:52 PM, kj <socyl@987jk.co m.invalidwrote:
          >>I know that I could rewrite the method like this:
          >>>
          >> def readline(self, size=None):
          >> if size == None:
          >> line = self.file.readl ine()
          >> else:
          >> line = self.file.readl ine(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.readl ine(*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

          Comment

          • Terry Reedy

            #6
            Re: overriding file.readline: &quot;an integer is required&quot;



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

            Comment

            Working...