[Tutor] use of __new__
denis.spir at gmail.com
Fri Mar 12 10:41:46 CET 2010
On Fri, 12 Mar 2010 12:27:02 +1100
Steven D'Aprano <steve at pearwood.info> wrote:
> 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)
I get it! The call to new must pass the target class as argument:
obj = unicode.__new__(Unicode, string, encoding)
while I passed unicode instead. I guess this is the bit that decides on the final class of the return obj.
> to this:
> # Untested
> if isinstance(string, unicode):
> # Don't do any decoding.
> obj = super(Unicode, cls).__new__(Unicode, string)
> if encoding is None: encoding = cls._ENCODING
> if errors is None: errors = cls._ERRORS
> obj = super(Unicode, cls).__new__(
> Unicode, string, encoding, errors)
Thank you again, Steven.
la vita e estrany
More information about the Tutor