type error on porting outfile.write

P

pmiller

I ported my code from the development to
application platform, I found a "type error"
on a fileout statement:

outfile.write(object.id +",")

Object.id is provided by a library routine
that is installed on both machines.

How do I fix this ?

Thanks,
Phil Miller
 
E

Eric McCoy

I ported my code from the development to
application platform, I found a "type error"
on a fileout statement:
outfile.write(object.id +",")

What is the type of object.id? I'm guessing an integer. The exception
should tell you, e.g.:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

If I'm right, you can do this:

outfile.write("%d," % (object.id))

Though probably the better solution is to find out what code is
assigning an unexpected value to object.id.
 
D

Dave Hansen

What is the type of object.id? I'm guessing an integer. The exception
should tell you, e.g.:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

If I'm right, you can do this:

outfile.write("%d," % (object.id))

Or, more generally,

outfile.write("%s, " % object.id)

or even (closer to the original code)

outfile.write(str(object.id)+", ")

Regards,
-=Dave
 
E

Eric McCoy

Dave said:
or even (closer to the original code)

outfile.write(str(object.id)+", ")

That was going to be my suggestion too, but that can mask the underlying
bug since a lot of types have __str__ methods. Not only could those
types theoretically return a valid stringified integer by coincidence,
if they don't you know nothing except "something went wrong." Getting a
TypeError would be much more useful for tracking down the bug.

Basically it would be shorthand for:

assert(isinstance(object.id, int))
outfile.write(str(object.id) + ", ")

Although I guess in his case, where the bug may be in a library over
which he has no control, it would be better to do:

assert(isinstance(object.id, int) or isinstance(object.id, str))
outfile.write("%s, " % (object.id))

since that code will run unmodified on both platforms he's using and
still give him error checking.
 

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
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top