Sub-classing unicode: getting the unicode value

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Dec 30 16:41:26 EST 2007


On 30 dic, 19:08, Torsten Bronger <bron... at physik.rwth-aachen.de>
wrote:
> Gabriel Genellina writes:
> > On 30 dic, 17:25, Torsten Bronger <bron... at physik.rwth-aachen.de>
> > wrote:
>
> >> 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:
>
> > The "actual value of the instance", given that it inherits from
> > unicode, is... self.
>
> But then it is not unicode but Excerpt which I don't want.  The idea
> is to buffer the unicode representation in order to gain efficiency.
> Otherwise, a lot of unicode conversion would take place.

Still I don't see why you want to inherit from unicode.

> > Are you sure you *really* want to inherit from unicode? Don't you
> > want to store an unicode object as an instance attribute?
>
> No, I need many unicode operations (concatenating, slicing, ...).

If you don't redefine __add__, __iadd__, __getitem__ etc. you'll end
up with bare unicode objects anyway; Excerpt + unicode = unicode. So
you'll have to redefine all required operators; then, why inherit from
unicode at all?
An example may help:

class Excerpt(object):
  def __init__(self, value):
    self.value = value # anything
  def __str__(self):
    return "%s(%r)" % (self.__class__.__name__, self.value)
  def __unicode__(self):
    if not hasattr(self, "_unicode"):
      self._unicode = unicode(self.value)
    return self._unicode
  def __add__(self, other):
    return Excerpt(unicode(self)+unicode(other))
  def __getitem__(self, index):
    return Excerpt(unicode(self)[index])

py> e1 = Excerpt((1,2,3))
py> e2 = Excerpt("Hello")
py> print e1
Excerpt((1, 2, 3))
py> print unicode(e1)
(1, 2, 3)
py> e3 = e1+e2
py> print e3
Excerpt(u'(1, 2, 3)Hello')
py> e3._unicode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Excerpt' object has no attribute '_unicode'
py> print e3[7:10]
Excerpt(u'3)H')
py> e3._unicode
u'(1, 2, 3)Hello'

> >> However, unicode(super(Excerpt, self)) is also forbidden because
> >> super() allows attribute access only (why by the way?).
> > (because its purpose is to allow cooperative methods in a multiple
> > inheritance hierarchy)
> It would be more useful, however, if it returned full-fledged
> objects.  Or if there was another way to get a full-fledged mother
> object.

There is no such "mother object", in Python an instance is usually a
whole unique object, not an onion-like structure.

--
Gabriel Genellina



More information about the Python-list mailing list