why is this failing?

C

Chris Curvey

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?
 
P

Peter Hansen

Chris said:
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
 
E

Erik Max Francis

Chris said:
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.
 
S

Skip Montanaro

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

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

Skip
 
G

Grant Edwards

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

John J. Lee

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
 
J

John J. Lee

Erik Max Francis said:
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
 
S

Skip Montanaro

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
 

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,435
Latest member
PhilipBelm

Latest Threads

Top