Message1706

Author lieske
Recipients
Date 2007-07-06.01:29:31
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
For a Python-defined class, it's possible to add a __repr__ method to the class which is invoked by the repr() built-in function.

That does not work for a Java-defined class.  Though one can define a __repr__ method, the method is not called by repr().

Example:

>>> import java.awt.geom
>>> e = java.awt.geom.Ellipse2D.Double(3, 4, 5, 6)
>>> e
java.awt.geom.Ellipse2D$Double@eb8007
### that is the standard Java Object.toString() of the instance

### Define a repr function for Ellipse2D
>>> def reprEllipse2D(self):
...     return "Ellipse2D(%g,%g,%g,%g)" % (self.x, self.y, self.width, self.height)

>>> reprEllipse2D(e)
'Ellipse2D(3,4,5,6)'


>>> java.awt.geom.Ellipse2D.Double.__repr__ = reprEllipse2D
>>> e
java.awt.geom.Ellipse2D$Double@eb8007
>>> repr(e)
'java.awt.geom.Ellipse2D$Double@eb8007'
### Jython is still using Object.toString()
>>> e.__repr__()
'Ellipse2D(3,4,5,6)'
### even though the method is accessible
History
Date User Action Args
2008-02-20 17:17:53adminlinkissue1748817 messages
2008-02-20 17:17:53admincreate