python - Accessing the original value of the unicode string when subclassing unicode -


try class work so:

>>> original = u"abcd-123-foo" >>> suffix = suffixcomparingunicodestring("foo") >>> suffix == original    # if original ends suffix, true true 

i know it's silly. please bear me.

anyway, can't figure out how actual, er, string (not str, mind) within unicode object. can fine:

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

how can without storing value myself? unicode call underlying character sequence?

since subclassing unicode, instances of sufficcomparingunicodestring can used other unicode string. can use other.endswith(self) in __eq__() implementation:

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

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -