Python 子类化unicode时访问unicode字符串的原始值

Python 子类化unicode时访问unicode字符串的原始值,python,string,python-2.7,subclass,python-unicode,Python,String,Python 2.7,Subclass,Python Unicode,尝试获得一个这样的课程: >>> original = u"ABCD-123-foo" >>> suffix = SuffixComparingUnicodeString("foo") >>> suffix == original # if original ends with suffix, True True 我知道这很傻。请容忍我 无论如何,我不知道如何从unicode对象中获取实际的字符串(不是str,注意)。我可以这样做很好

尝试获得一个这样的课程:

>>> original = u"ABCD-123-foo"
>>> suffix = SuffixComparingUnicodeString("foo")
>>> suffix == original    # if original ends with suffix, True
True
我知道这很傻。请容忍我

无论如何,我不知道如何从
unicode
对象中获取实际的字符串(不是
str
,注意)。我可以这样做很好:

>>> class SuffixComparingUnicodeString(unicode):
...     def __init__(self, x):
...          super(SuffixComparingUnicodeString, self).__init__(x)
...          self._myval = x
...     def __eq__(self, other):
...          return isinstance(other, unicode) and other.endswith(self._myval)
...
>>> SuffixComparingUnicodeString("foo") == u"barfoo"
True

如果我自己不存储该值,我怎么做呢?
unicode
将其底层字符序列称为什么?

由于您是一个子类化的
unicode
suffeComparingUnicode>的实例通常可以像任何其他unicode字符串一样使用。因此,您可以在
\uu eq\uuu()实现中使用
other.endswith(self)

class SuffixComparingUnicodeString(unicode):
    def __eq__(self, other):
        return isinstance(other, unicode) and other.endswith(self)

对不起,你有什么问题?如何获取用于初始化的字符串的非unicode版本?
unicode
调用其“基础字符串”
self
。这就像试图从
3
对象中获取
3
。如果你想要一个
unicode
的实例,你可以使用
unicode(self)
@agf我在问:如果你有
myuni=unicode(“foo”)
,unicode
的成员在
myuni
实例中用什么来谈论
u“foo”
?我很难清楚地表达出来,但是,我觉得我的问题很清楚。