What could generate the following behavior ?
>>> print str(msg)
my message
>>> print unicode(msg)
my message
But:
>>> print '%s' % msg
another message
More info:
- my
msg
object is inherited fromunicode
. - the methods
__str__
/__unicode__
/__repr__
methods were overridden to return the string'my message'
. - the
msg
object was initialised with the string'another message'
. - this is running on python 2.5
- the variable
msg
was not changed between the tests - this is actually real doctest that is really giving these results.
I would like an solution that matches this doctest, with minimal fuss (especially around the actual inheritance):
>>> print '%s' % msg
my message
Thanks for all suggestions.
I don't feel this will help more, but for curious readers (and adventurous pythonist), here's the implementation of the object:
class Message(zope.i18nmessageid.Message):
def __repr__(self):
return repr(zope.i18n.interpolate(self.default, self.mapping))
def __str__(self):
return zope.i18n.interpolate(self.default, self.mapping)
def __unicode__(self):
return zope.i18n.interpolate(self.default, self.mapping)
This is how we create the object msg:
>>> msg = Message('another message', 'mydomain', default='my message')
Zope packages version and code used are:
- zope.i18n-3.4.0 (interpolation() method code )
- zope.i18nmessageid-3.4.3 (Message class code)
EDIT INFO:
- added/updated the names of the methods that were overriden
- added some more info (python version, and minor info)
- updated some wrong info (the class of `msg` is based on `unicode` class and not `basestring`)
- added the actual implementation of the class used