469,645 Members | 1,148 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

type error on porting outfile.write

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

Dec 20 '05 #1
3 2733
pm*****@gnf.org wrote:
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.

Dec 21 '05 #2
On Tue, 20 Dec 2005 22:11:01 -0500 in comp.lang.python, Eric McCoy
<ct******@comcast.net> wrote:
pm*****@gnf.org wrote:
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))


Or, more generally,

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

or even (closer to the original code)

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

Regards,
-=Dave

--
Change is inevitable, progress is not.
Dec 21 '05 #3
Dave Hansen wrote:
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.

Dec 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by darrel | last post: by
reply views Thread by Curtiss | last post: by
reply views Thread by pmiller | last post: by
3 posts views Thread by Alden Pierre | last post: by
669 posts views Thread by Xah Lee | last post: by
13 posts views Thread by imutate | last post: by
2 posts views Thread by unclefester | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.