Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12529 > unrolled thread

Re: Subclassing str object

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2011-08-31 16:16 -0600
Last post2011-08-31 16:16 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Subclassing str object Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-31 16:16 -0600

#12529 — Re: Subclassing str object

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-08-31 16:16 -0600
SubjectRe: Subclassing str object
Message-ID<mailman.633.1314829046.27778.python-list@python.org>
2011/8/31 Yaşar Arabacı <yasar11732@gmail.com>:
> @Ian: Thanks for you comments. I indeed didn't need the _sozcuk attribute at
> all, so I deleted it. My class's addition and multiplication works without
> overwriting __add__ and __mul__ because, this class uses unicode's __add__
> and __mul__ than creates a new kelime instance with return value of those
> methods in __getattribute__.

I think if you try it, you'll find that the result is an ordinary
unicode object, not a kelime instance, because __getattribute__ is
*not* invoked when Python looks up special method names on the class
object.

> I didn't get a good grasp on how using basestring there might broke
> encoding, could you explain a little bit more, or provide a reading
> material?

The unicode.encode method takes a unicode object and encodes it into a
byte string (a str object).  If you then wrap that up in a kelime
object, which is a unicode subclass, it has to decode the string back
to unicode (using the default ascii codec, since it isn't specified).
Thus the result of the call is no longer an encoded byte string as
would be expected.  If you're lucky, you'll get a UnicodeDecodeError
since it's just using the ascii codec.  If you're unlucky, it will
silently return a result of the wrong type.

> So the thing I wonder, when creating new instance in for example
> capitalize() method, does str use something like self.__new__() or
> unicode.__new__()? Because, in latter case, I could override the __new__
> method on my class, so that every method would create my class's instance,
> instead of unicode's

No, that doesn't work.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web