[Tutor] use of __new__

Steven D'Aprano steve at pearwood.info
Fri Mar 12 04:04:51 CET 2010


On Fri, 12 Mar 2010 06:03:35 am spir wrote:
> Hello,
>
> I need a custom unicode subtype (with additional methods).
[snip]

Here's my second attempt, and a very simple test function that passes. 
Obviously you have to add your own additional methods :)


class Unicode(unicode):
    """Unicode(string [, encoding[, errors]]) -> object

    Special Unicode class that has all sorts of wonderful
    methods missing from the built-in unicode class.
    """
    _ENCODING = "utf8"
    _ERRORS = "strict"
    def __new__(cls, string='', encoding=None, errors=None):
        optional_args = not (encoding is errors is None)
        # Set default encoding and errors.
        if encoding is None:  encoding = cls._ENCODING
        if errors is None:  errors = cls._ERRORS
        # To match the behaviour of built-in unicode, if either
        # optional argument is specified, we always attempt decoding.
        if optional_args or not isinstance(string, unicode):
            args = (string, encoding, errors)
        else:
            args = (string,)
        return super(Unicode, cls).__new__(Unicode, *args)


def test():
    assert Unicode() == u''
    assert Unicode('abcd') == u'abcd'
    u = 'cdef'.decode('utf-16')
    assert u == u'\u6463\u6665'
    s = u.encode('utf-8')
    assert Unicode(s) == u
    try:
        unicode(s)
    except UnicodeDecodeError:
        pass
    else:
        assert False, 'failed to fail as expected'



-- 
Steven D'Aprano


More information about the Tutor mailing list