why is this failing?

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

    why is this failing?

    I've looked at this so long that I must be looking right past it. This
    code:

    class Page:
    ############### ############### ############### #############
    def write(self, value, row=None, col=None, len=None):
    print isinstance(valu e, str)

    # error occurs on this line
    print len(value)

    ############### ############### ############### ###########
    if __name__ == "__main__":
    p = Page()

    p.write("banana ")



    is throwing this error:




    True
    Traceback (most recent call last):
    File "P2.py", line 13, in ?
    p.write("banana ")
    File "P2.py", line 7, in write
    print len(value)
    TypeError: 'NoneType' object is not callable


    What am I missing?

  • Erik Max Francis

    #2
    Re: why is this failing?

    Chris Curvey wrote:
    [color=blue]
    > I've looked at this so long that I must be looking right past it.
    > This
    > code:
    >
    > class Page:
    > ############### ############### ############### #############
    > def write(self, value, row=None, col=None, len=None):[/color]
    ^^^[color=blue]
    > print isinstance(valu e, str)
    >
    > # error occurs on this line
    > print len(value)[/color]

    You are overriding the name len, so this is equivalent to writing

    None(value)

    which is obviously an error.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ God is love, but get it in writing.
    \__/ Gypsy Rose Lee

    Comment

    • Peter Hansen

      #3
      Re: why is this failing?

      Chris Curvey wrote:[color=blue]
      >
      > I've looked at this so long that I must be looking right past it. This
      > code:
      >
      > class Page:
      > ############### ############### ############### #############
      > def write(self, value, row=None, col=None, len=None):
      > print isinstance(valu e, str)
      >
      > # error occurs on this line
      > print len(value)
      >
      > ############### ############### ############### ###########
      > if __name__ == "__main__":
      > p = Page()
      >
      > p.write("banana ")
      >
      > is throwing this error:
      >
      > True
      > Traceback (most recent call last):
      > File "P2.py", line 13, in ?
      > p.write("banana ")
      > File "P2.py", line 7, in write
      > print len(value)
      > TypeError: 'NoneType' object is not callable[/color]

      Reading the error more closely, and examining the line that is failing,
      you can see that the only call being made is to len(), but the error is
      telling you that the name "len" is actually bound to None! How could
      that happen? Check your list of arguments to the write() method...

      Don't use names of builtins and your life will be easier. :-)

      -Peter

      Comment

      • Grant Edwards

        #4
        Re: why is this failing?

        In article <3F60E645.2501B 9EE@engcorp.com >, Peter Hansen wrote:
        [color=blue]
        > Don't use names of builtins and your life will be easier. :-)[/color]

        And the easiest way to do that is to use an editor with a
        Python mode. Make sure it shows builtins in a distinct color.
        You will then know immediately (while editing) when you
        accidentally redefine a builtin.

        --
        Grant Edwards grante Yow! I call it a "SARDINE
        at ON WHEAT"!
        visi.com

        Comment

        • Skip Montanaro

          #5
          Re: why is this failing?


          Chris> def write(self, value, row=None, col=None, len=None):

          You overrode the len() builtin in your function signature ^^^.

          Skip

          Comment

          • John J. Lee

            #6
            Re: why is this failing?

            grante@visi.com (Grant Edwards) writes:
            [color=blue]
            > In article <3F60E645.2501B 9EE@engcorp.com >, Peter Hansen wrote:
            >[color=green]
            > > Don't use names of builtins and your life will be easier. :-)[/color]
            >
            > And the easiest way to do that is to use an editor with a
            > Python mode. Make sure it shows builtins in a distinct color.
            > You will then know immediately (while editing) when you
            > accidentally redefine a builtin.[/color]

            You wouldn't happen to know how to do this for python-mode?

            emacs-illiterate-ly y'rs,


            John

            Comment

            • John J. Lee

              #7
              Re: why is this failing?

              Erik Max Francis <max@alcyone.co m> writes:
              [color=blue]
              > "John J. Lee" wrote:
              >[color=green]
              > > You wouldn't happen to know how to do this for python-mode?[/color]
              >
              > The latest versions will do it automatically.[/color]

              Just to check (I'm using a patched version, so it's a bit of a pain to
              switch): the latest versions from the python-mode SF project highlight
              builtins like "len" and "list", not just keywords like "print"?


              John

              Comment

              • Skip Montanaro

                #8
                Re: why is this failing?


                John> Just to check (I'm using a patched version, so it's a bit of a
                John> pain to switch): the latest versions from the python-mode SF
                John> project highlight builtins like "len" and "list", not just
                John> keywords like "print"?

                I'm running 4.38. Here are the keywords it knows about:

                "and" "assert" "break" "class"
                "continue" "def" "del" "elif"
                "else" "except" "exec" "for"
                "from" "global" "if" "import"
                "in" "is" "lambda" "not"
                "or" "pass" "print" "raise"
                "return" "while" "yield"

                So, no, the builtins are not colored, just the keywords. You could easily
                extend the list though.

                Skip


                Comment

                Working...