Sub-classing unicode: getting the unicode value

Torsten Bronger bronger at physik.rwth-aachen.de
Sun Dec 30 14:25:26 EST 2007


Hallöchen!

I sub-classed unicode in an own class called "Excerpt", and now I
try to implement a __unicode__ method.  In this method, I want to
get the actual value of the instance, i.e. the unicode string:

    def __unicode__(self):
        """Returns the Unicode representation of Excerpt.  Note that this is buffered,
        so don't be afraid of calling it many times.

        :Return:
          - Unicode representation of ``self``

        :rtype: unicode
        """
        if not hasattr(self, "__unicode"):
            self.__unicode = super(Excerpt, self).__unicode__()
        return self.__unicode

Unfortunately, unicode objects don't have a __unicode__ method.
However, unicode(super(Excerpt, self)) is also forbidden because
super() allows attribute access only (why by the way?).

How does my object get its own value?

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                                      Jabber ID: bronger at jabber.org
               (See http://ime.webhop.org for further contact info.)



More information about the Python-list mailing list