471,336 Members | 1,297 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

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(value, 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?

Jul 18 '05 #1
7 1464
Chris Curvey wrote:
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(value, str)

# error occurs on this line
print len(value)


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

None(value)

which is obviously an error.

--
Erik Max Francis && ma*@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
Jul 18 '05 #2
Chris Curvey wrote:

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(value, 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


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
Jul 18 '05 #3
In article <3F***************@engcorp.com>, Peter Hansen wrote:
Don't use names of builtins and your life will be easier. :-)


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
Jul 18 '05 #4

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

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

Skip

Jul 18 '05 #5
gr****@visi.com (Grant Edwards) writes:
In article <3F***************@engcorp.com>, Peter Hansen wrote:
Don't use names of builtins and your life will be easier. :-)


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.


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

emacs-illiterate-ly y'rs,
John
Jul 18 '05 #6
Erik Max Francis <ma*@alcyone.com> writes:
"John J. Lee" wrote:
You wouldn't happen to know how to do this for python-mode?


The latest versions will do it automatically.


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
Jul 18 '05 #7

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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon Stewart | last post: by
2 posts views Thread by Per Bergland | last post: by
2 posts views Thread by Dung Ping | last post: by
1 post views Thread by Frank Rizzo | last post: by
6 posts views Thread by Adam Honek | last post: by
6 posts views Thread by laredotornado | last post: by
reply views Thread by meethari1984 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.