By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

equivalent to Java's toString()?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.