equivelant of Java's toString() method? (not repr)

A

Alex Hunsley

Pretty simple seeming question, but can't find answer via google or docs...

I am using urllib2 as follows:

handle = urlopen(req, postdata) # and URL to return a handle
on
...
print handle.info()

the print statement prints out the headers:

Content-Type: text/html;charset=ISO-8859-1
Content-Length: 2845
Date: Tue, 01 Feb 2005 12:40:28 GMT
Server: Apache Coyote/1.0
Connection: close

which is the string I want to use.
However, if I write a function call using this:

log(handle.info())

I get this:

TypeError: cannot concatenate 'str' and 'instance' objects

(The log function expects a string)
What is the magic incantation to get the string I want so I can pass it
to my function?
I've tried repr() but that isn't it (I suspected it wouldn't be).
I suppose I'm looking for the equivelant of Java's toString() method...

thanks!
alex
 
A

Alex Hunsley

Alex said:
Pretty simple seeming question, but can't find answer via google or docs...

I am using urllib2 as follows:

handle = urlopen(req, postdata) # and URL to return a handle on
...
print handle.info()

the print statement prints out the headers:

Content-Type: text/html;charset=ISO-8859-1
Content-Length: 2845
Date: Tue, 01 Feb 2005 12:40:28 GMT
Server: Apache Coyote/1.0
Connection: close

which is the string I want to use.
However, if I write a function call using this:

log(handle.info())

I get this:

TypeError: cannot concatenate 'str' and 'instance' objects

(The log function expects a string)
What is the magic incantation to get the string I want so I can pass it
to my function?
I've tried repr() but that isn't it (I suspected it wouldn't be).
I suppose I'm looking for the equivelant of Java's toString() method...

thanks!
alex

Replying to meself...
D'oh! Just found the str() function which is just what I need!

log(str(handle.info())) works fine....

thx
alex
 
R

Roy Smith

Alex Hunsley said:
I suppose I'm looking for the equivelant of Java's toString() method...

That would be str(), which for the most part, just calls your object's
__str__() method. If your object doesn't have an __str__() method, there's
nothing magic Python can do to invent one.

The difference between repr() and str() is that repr() is supposed to
produce a parsable representation of the object; one which could be parsed
by a Python interpreter to re-generate the original object. On the other
hand, str() is supposed to produce a human-readable string.
 
O

Ola Natvig

Alex said:
Pretty simple seeming question, but can't find answer via google or docs...

I am using urllib2 as follows:

handle = urlopen(req, postdata) # and URL to return a handle on
...
print handle.info()

the print statement prints out the headers:

Content-Type: text/html;charset=ISO-8859-1
Content-Length: 2845
Date: Tue, 01 Feb 2005 12:40:28 GMT
Server: Apache Coyote/1.0
Connection: close

which is the string I want to use.
However, if I write a function call using this:

log(handle.info())

I get this:

TypeError: cannot concatenate 'str' and 'instance' objects

(The log function expects a string)
What is the magic incantation to get the string I want so I can pass it
to my function?
I've tried repr() but that isn't it (I suspected it wouldn't be).
I suppose I'm looking for the equivelant of Java's toString() method...

thanks!
alex

use:

log(str(handle.info()))

str creates a string object from a supplied object with a __str__
method. I think print calls str on what it is going to print
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top