[Tutor] use of __new__

Steven D'Aprano steve at pearwood.info
Fri Mar 12 02:27:02 CET 2010


On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote:

> I have tried to match the behaviour of the built-in unicode as close
> as I am able. See here:
> http://docs.python.org/library/functions.html#unicode

And by doing so, I entirely forgot that you want to change the default 
encoding from 'ascii' to 'utf-8'! Oops. Sorry about that.

Try changing this bit:

>         else:  # Never attempt decoding.
>             obj = super(Unicode, cls).__new__(Unicode, string)

to this:

        # Untested
        else:
            if isinstance(string, unicode):
                # Don't do any decoding.
                obj = super(Unicode, cls).__new__(Unicode, string)
            else:
                if encoding is None: encoding = cls._ENCODING
                if errors is None: errors = cls._ERRORS
                obj = super(Unicode, cls).__new__(
                  Unicode, string, encoding, errors)


You can probably clean up the method to make it a bit tidier.



-- 
Steven D'Aprano


More information about the Tutor mailing list