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

Ola Natvig ola.natvig at infosense.no
Tue Feb 1 08:28:56 EST 2005


Alex Hunsley wrote:
> 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



-- 
--------------------------------------
  Ola Natvig <ola.natvig at infosense.no>
  infoSense AS / development



More information about the Python-list mailing list