473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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


Jul 18 '05 #1
3 4373
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


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

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

thx
alex
Jul 18 '05 #2
Alex Hunsley <la**@tardis.ed.ac.molar.uk> wrote:
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.
Jul 18 '05 #3
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 <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
3
by: Gert Schumann | last post by:
I'm operating on sun OS 5.6 I ping a host every 10 seconds to get knowlegde wheather it is running or not. After about one and a half hour I get this Exception: java.io.IOException: Too many open...
3
by: Gabriel Cooper | last post by:
What is the python equivalent to java's toString()? When debugging I want to be able to basically be able to do this: print MyObjectInstance or print str(MyObjectInstance) and have it...
1
by: henrymania | last post by:
Am writing a code for database backup....by backupservlet is as given below i get the following exception
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
47
by: Nathan Sokalski | last post by:
VB.NET has a function, Asc(), that gets the Ascii value of a character. I am attempting to find an equivelant function for C#. Can somebody help me here? -- Nathan Sokalski njsokalski@hotmail.com...
7
by: =?Utf-8?B?Y291Z2FyaXN0aWM=?= | last post by:
I am trying to convert an C# application to VB and have one issue which is converting the generic value to the default. C# uses return default(T) as the return value how would I translate this in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.