Strange problem when running python code
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Wed Apr 5 10:12:47 EDT 2006
On Tue, 04 Apr 2006 12:01:12 -0700, ishtar2020 wrote:
> I must add, when the python interpreter displays the traceback, with
> the line that is producing the error, it doesn't look like the one I
> got in the code.
I sometimes get that problem when I'm running code, I make a change in the
source file, use reload() to update my module, but forget to reinitialise
my objects still in memory.
So I end up with (buggy) instances in memory, but when I hit a traceback,
the source code displayed has nothing to do with the actual error because
the source file has been edited.
If you are getting these weird errors when you do nothing but change
comments, there is a good chance that's what is happening.
Here is an example of that behaviour. Is this the sort of thing which is
happening to you?
# === File tester.py ===
class Parrot(object):
def speak(self):
print "I'm pining for the fjords."
def species(self, colour=None):
return "Norwegian " + colour
Now import the file:
>>> import tester
>>> p = tester.Parrot()
>>> p.speak()
I'm pining for the fjords.
>>> p.species()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "tester.py", line 9, in species
return "Norwegian " + colour
TypeError: cannot concatenate 'str' and 'NoneType' objects
Now I edit the file, just adding comments and nothing else:
# === File tester.py ===
class Parrot(object):
# Add some comments here.
# Line two.
def speak(self):
print "I'm pining for the fjords."
def species(self, colour=None):
return "Norwegian " + colour
But if I execute the old existing object, I get a nonsensical error:
>>> p.species()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "tester.py", line 9, in species
print "I'm pining for the fjords."
TypeError: cannot concatenate 'str' and 'NoneType' objects
Even doing a reload() doesn't help. I have to delete the old instance, and
create a new one.
--
Steven.
More information about the Python-list
mailing list