Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:39:49+00:00 2026-05-13T19:39:49+00:00

What could generate the following behavior ? >>> print str(msg) my message >>> print

  • 0

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 from unicode.
  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:39:50+00:00Added an answer on May 13, 2026 at 7:39 pm

    Update 2: Please find the original answer, including a simple example of a class exhibiting the behaviour described by the OP, below the horizontal bar. As for what I was able to surmise in the course of my inquiry into Python’s sources (v. 2.6.4):

    The file Include/unicodeobject.h contains the following to lines (nos. 436-7 in my (somewhat old) checkout):

    #define PyUnicode_AS_UNICODE(op) \                                              
            (((PyUnicodeObject *)(op))->str)
    

    This is used all over the place in the formatting code, which, as far as I can tell, means that during string formatting, any object which inherits from unicode will be reached into so that its unicode string buffer may be used directly, without calling any Python methods. Which is good as far as performance is concerned, I’m sure (and very much in line with Juergen’s conjecture in a comment on this answer).

    For the OP’s question, this probably means that making things work the way the OP would like them to may only be possible if something like Anurag Uniyal’s wrapper class idea is acceptable for this particular use case. If it isn’t, the only thing which comes to my mind now is to wrap objects of this class in str / unicode wherever their being interpolated into a string… ugh. (I sincerely hope I’m just missing a cleaner solution which someone will point out in a minute!)


    (Update: This was posted about a minute before the OP included the code of his class, but I’m leaving it here anyway (1) for the conjecture / initial attempt at an explanation below the code, (2) for a simple example of how to produce this behaviour (Anurag Uniyal has since provided another one calling unicode‘s constructor directly, as opposed to via super), (3) in hope of later being able to edit in something to help the OP in obtaining the desired behaviour.)

    Here’s an example of a class which actually works like what the OP describes (Python 2.6.4, it does produce a deprecation warning — /usr/bin/ipython:3: DeprecationWarning: object.__init__() takes no parameters):

    class Foo(unicode):
        def __init__(self, msg):
            super(unicode, self).__init__(msg)
        def __str__(self): return 'str msg'
        def __repr__(self): return 'repr msg'
        def __unicode__(self): return u'unicode msg'
    

    A couple of interactions in IPython:

    In [12]: print(Foo("asdf"))
    asdf
    
    In [13]: str(Foo("asdf"))
    Out[13]: 'str msg'
    
    In [14]: print str(Foo("asdf"))
    -------> print(str(Foo("asdf")))
    str msg
    
    In [15]: print(str(Foo("asdf")))
    str msg
    
    In [16]: print('%s' % Foo("asdf"))
    asdf
    

    Apparently string interpolation treats this object as an instance of unicode (directly calling the unicode implementation of __str__), whereas the other functions treat it as an instance of Foo. How this happens internally and why it works like this and whether it’s a bug or a feature, I really don’t know.

    As for how to fix the OP’s object… Well, how would I know without seeing its code??? Give me the code and I promise to think about it! Ok, I’m thinking about it… No ideas so far.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 319k
  • Answers 319k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you are setting your DataList1.EditItemIndex this applies to the… May 14, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer UPDATED DEMO: http://jsbin.com/epeti3/5 /* 16/02/2012 02.04.38 */ (function($) { $.fn.jStackOverflow… May 14, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer A workaroud would be to use a sub-query: SELECT FieldA,… May 14, 2026 at 12:04 am

Related Questions

I have subclassed Form to include some extra functionality, which boils down to a
My SAAJ-based client generates the following XML and sends it to a .NET web
We have a rather large application my team and I are developing that contains
I'm using Prawn and Prawnto to generate a PDF in a Ruby on Rails
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.