equivalent to Java's toString()?

Discussion in 'Python' started by Gabriel Cooper, Apr 19, 2004.

  1. 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);
    }
    }
     
    Gabriel Cooper, Apr 19, 2004
    #1
    1. Advertising

  2. 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
     
    Michael Geary, Apr 19, 2004
    #2
    1. Advertising

  3. Gabriel Cooper

    Roy Smith Guest

    In article <>,
    "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


    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.
     
    Roy Smith, Apr 19, 2004
    #3
  4. Gabriel Cooper

    Ype Kingma Guest

    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/org/python/core/PyObject.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
     
    Ype Kingma, Apr 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ph. Barthelemy

    toString equivalent for array

    Ph. Barthelemy, Oct 13, 2004, in forum: Java
    Replies:
    13
    Views:
    15,204
    Tony Morris
    Oct 14, 2004
  2. Fredrik Lundh

    Re: equivalent to Java's toString()?

    Fredrik Lundh, Apr 19, 2004, in forum: Python
    Replies:
    0
    Views:
    439
    Fredrik Lundh
    Apr 19, 2004
  3. Alex Hunsley
    Replies:
    3
    Views:
    478
    Ola Natvig
    Feb 1, 2005
  4. Greg B
    Replies:
    10
    Views:
    1,539
    Faton Berisha
    May 9, 2007
  5. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    635
    Fergus McMenemie
    Jul 17, 2012
Loading...

Share This Page