473,407 Members | 2,598 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,407 software developers and data experts.

equivalent to Java's toString()?

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 print out formatted output along the lines of:

Object properties: Red=0 Yellow=0 Blue=255

currently I get something like this:

<Some Object at 0xff32ffca3>

In java it would look like this:

class SomeClass {
[...]
String toString() {
return new String("Object properties: Red=" + red + " Yellow: " +
yellow + " Blue: " + blue);
}
}

Jul 18 '05 #1
3 114684
Gabriel Cooper wrote:
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 print out formatted output along the lines of:

Object properties: Red=0 Yellow=0 Blue=255


Define a __str__ method in your class. It works just like toString() in Java
and javascript:
class MyTest( object ): .... def __str__( self ):
.... return 'My Test!'
.... test = MyTest()
print test My Test!


Also see __repr__ for a related method.

-Mike
Jul 18 '05 #2
In article <10*************@corp.supernews.com>,
"Michael Geary" <Mi**@DeleteThis.Geary.com> wrote:
Gabriel Cooper wrote:
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 print out formatted output along the lines of:

Object properties: Red=0 Yellow=0 Blue=255


Define a __str__ method in your class. It works just like toString() in Java
and javascript:
class MyTest( object ): ... def __str__( self ):
... return 'My Test!'
... test = MyTest()
print test My Test!


Also see __repr__ for a related method.

-Mike


Also, you might want to look at the pprint module. It's not quite what
you were asking for, but it's worth knowing about if you're doing this
kind of stuff.
Jul 18 '05 #3
Michael Geary wrote:
Gabriel Cooper wrote:
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 print out formatted output along the lines of:

Object properties: Red=0 Yellow=0 Blue=255
Define a __str__ method in your class. It works just like toString() in

Java and javascript:
class MyTest( object ): ... def __str__( self ):
... return 'My Test!'
... test = MyTest()
print test My Test!


Also see __repr__ for a related method.

-Mike


Closing the circle, just for info, from the java docs of jython:

http://www.jython.org/docs/javadoc/o....html#__str__()

"""Equivalent to the standard Python __str__ method. This method should not
typically need to be overridden. The easiest way to configure the string
representation of a PyObject is to override the standard Java toString
method."""

Regards,
Ype

Jul 18 '05 #4

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

Similar topics

0
by: Samir Patel | last post by:
To increase pythons' popularity, we need a tool like webstart which will install (and update when needed) python program through web. This will make IT and managers happy since they don't have to...
8
by: Alvin Bruney | last post by:
I was taught at aspnetpro seminar (which the company paid good bucks for) that tostring doesn't actually create a string object or that it doesn't do a conversion of the specified value to its...
2
by: Rod Brick | last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not...
14
by: dale zhang | last post by:
Hi groups, Can anyone give me the equivalent C# sharp code for this VB.ET code, :: VB.NET :: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles...
13
by: hazz | last post by:
I need to get a set of comma delimited client ids from a config file to test against while running through my main processing loop. If a given clientid matches any of the ids in the list, I don't...
11
by: Philipp Brune | last post by:
Hello Newsgroup, in c# there exists the '??' operator. It is used like this : string s1 = null; string s2 = s1 ?? "(undefined)" And does the following (simplified) :
3
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String...
2
by: Dan | last post by:
What is the equivalent of Double.toString from Java in C# In Java new Double(4).toString() = "4.0" but in C# new Double(4).ToString() = "4"
11
by: Andreas Wollschlaeger | last post by:
Hi folks, as the subject says, i'm a poor Java programmer trying to transfer some of his wisdom into C++ world... here is what im trying to do this evening: Java has a nifty feature called a...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.