Str and instance concatanation error

Bengt Richter bokr at oz.net
Mon Nov 4 19:55:07 EST 2002


On Mon, 04 Nov 2002 23:01:05 GMT, "Newt" <newt_e at blueyonder.co.uk> wrote:

>I keep getting the following error when trying to write to a file.
>
>Traceback (most recent call last):
>  File "C:\Python22\lib\lib-tk\Tkinter.py", line 1292, in __call__
>    return apply(self.func, args)
>  File "D:\py_stuff\curry.py", line 14, in __call__
>    return self.fun(*(self.pending + args), **kw)
>  File "D:\py_stuff\chaz.py", line 144, in savefile
>    gv.save_char()
>  File "D:\py_stuff\globals.py", line 96, in save_char
>    file.write('<creator>' + self.creator + '</creator>')
>TypeError: cannot concatenate 'str' and 'instance' objects
>
>This occurs in a class module (called globals.py). This is used to hold all
>my global variables, and also has a method (called save_char) to write these
>to a file.
>
>self.creator is initialised in the __init__ as "". Should I create it as a
>stringVar instead, or am I doing something incredibly stupid.
>
Well, the error message is a strong clue, but if it doesn't make sense,
what do you do when you don't know why something like self.creator is causing
a problem (besides post a question ;-) ? I'm not being sarcastic, I'm wondering
why you wouldn't just plunk in a print statement to see what everything involved
is. You say you think it is initialized to as "", which shouldn't cause a problem.
So what could it have become so that it does cause a problem? What can you do to
find out what it is at the point where it causes the problem?

Well, make the program tell you more. E.g., just before the file.write above, put

    print 'self.creator =', `self.creator`

or
    print 'self.creator =', repr(self.creator)

(I recommend the back ticks or repr just to make sure you get something you can see,
like '' for a null string etc.)

If you mustn't touch the code, make a safe copy, and verify that you've restored it
afterwards, or make a wholesale hacking copy and just mess with it any way that forces
it to tell you what you want to know.

Often, it will be less typing and faster than posting a question.
HTH

Regards,
Bengt Richter



More information about the Python-list mailing list