Str and instance concatanation error

Newt newt_e at blueyonder.co.uk
Tue Nov 5 14:09:45 EST 2002


"Bengt Richter" <bokr at oz.net> wrote in message
news:aq74tb$idp$0 at 216.39.172.122...
> 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.
<snip>
> >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

I didn't plunk in a print statement as I thought the problem was within my
class module, or something strange/supid I'd done.I expected to get the same
sort of error. However, I plunked in a print statement, and strangely got
back a number. I then realised what the problem was. It's got nothing to do
with my class module.

I have a couple of functions to set or get the value of creator. I also have
another module that imports my globals.py
The upshot is that I called my set_creator function with the return from an
Entry command (which is the instance of the Entry command, and not the text
that is typed in (once I added the .get() to the end it all worked fine!).

Newt






More information about the Python-list mailing list