468,296 Members | 1,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 113901
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Alvin Bruney | last post: by
14 posts views Thread by dale zhang | last post: by
11 posts views Thread by Philipp Brune | last post: by
2 posts views Thread by Dan | last post: by
11 posts views Thread by Andreas Wollschlaeger | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.