equivalent to Java's toString()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Cooper

    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 MyObjectInstanc e

    or
    print str(MyObjectIns tance)

    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);
    }
    }

  • Michael Geary

    #2
    Re: equivalent to Java's toString()?

    Gabriel Cooper wrote:[color=blue]
    > What is the python equivalent to java's toString()?
    >
    > When debugging I want to be able to basically be able to do this:
    >
    > print MyObjectInstanc e
    >
    > or
    > print str(MyObjectIns tance)
    >
    > and have it print out formatted output along the lines of:
    >
    > Object properties: Red=0 Yellow=0 Blue=255[/color]

    Define a __str__ method in your class. It works just like toString() in Java
    and JavaScript:
    [color=blue][color=green][color=darkred]
    >>> class MyTest( object ):[/color][/color][/color]
    .... def __str__( self ):
    .... return 'My Test!'
    ....[color=blue][color=green][color=darkred]
    >>> test = MyTest()
    >>> print test[/color][/color][/color]
    My Test![color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Also see __repr__ for a related method.

    -Mike


    Comment

    • Roy Smith

      #3
      Re: equivalent to Java's toString()?

      In article <1087t9hc5vl833 b@corp.supernew s.com>,
      "Michael Geary" <Mike@DeleteThi s.Geary.com> wrote:
      [color=blue]
      > Gabriel Cooper wrote:[color=green]
      > > What is the python equivalent to java's toString()?
      > >
      > > When debugging I want to be able to basically be able to do this:
      > >
      > > print MyObjectInstanc e
      > >
      > > or
      > > print str(MyObjectIns tance)
      > >
      > > and have it print out formatted output along the lines of:
      > >
      > > Object properties: Red=0 Yellow=0 Blue=255[/color]
      >
      > Define a __str__ method in your class. It works just like toString() in Java
      > and JavaScript:
      >[color=green][color=darkred]
      > >>> class MyTest( object ):[/color][/color]
      > ... def __str__( self ):
      > ... return 'My Test!'
      > ...[color=green][color=darkred]
      > >>> test = MyTest()
      > >>> print test[/color][/color]
      > My Test![color=green][color=darkred]
      > >>>[/color][/color]
      >
      > Also see __repr__ for a related method.
      >
      > -Mike[/color]

      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.

      Comment

      • Ype Kingma

        #4
        Re: equivalent to Java's toString()?

        Michael Geary wrote:
        [color=blue]
        > Gabriel Cooper wrote:[color=green]
        >> What is the python equivalent to java's toString()?
        >>
        >> When debugging I want to be able to basically be able to do this:
        >>
        >> print MyObjectInstanc e
        >>
        >> or
        >> print str(MyObjectIns tance)
        >>
        >> and have it print out formatted output along the lines of:
        >>
        >> Object properties: Red=0 Yellow=0 Blue=255[/color]
        >
        > Define a __str__ method in your class. It works just like toString() in[/color]
        Java[color=blue]
        > and JavaScript:
        >[color=green][color=darkred]
        >>>> class MyTest( object ):[/color][/color]
        > ... def __str__( self ):
        > ... return 'My Test!'
        > ...[color=green][color=darkred]
        >>>> test = MyTest()
        >>>> print test[/color][/color]
        > My Test![color=green][color=darkred]
        >>>>[/color][/color]
        >
        > Also see __repr__ for a related method.
        >
        > -Mike[/color]

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

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

        """Equivale nt 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



        Comment

        Working...