@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__.<div>
<br></div><div>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?</div><div><br></div><div>And, as with the purpose, yes, it is intended to add some methods on unicode. But the bigger purpose is to learn how to work with builtin objects, or more spesifically, immutable ones. Therefore I value much to make this simple example work, and make it work in a python way.</div>
<div><br></div><div>@Terry: yes, my *args **kwargs method made tracebacks much less informative. I agree on that. I am still trying to implement a better way.</div><div><br></div><div>@all:</div><div><br></div><div>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</div>
<div><br></div><div><br><br><div class="gmail_quote">31 Ağustos 2011 20:11 tarihinde Ian Kelly <span dir="ltr"><<a href="mailto:ian.g.kelly@gmail.com">ian.g.kelly@gmail.com</a>></span> yazdı:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
2011/8/31 Yaşar Arabacı <<a href="mailto:yasar11732@gmail.com">yasar11732@gmail.com</a>>:<br>
<div class="im">> I made a class like this (I shortened it just to show the point), what do<br>
> you think about it, do you think it is the python way of subclassing str (or<br>
> unicode in this case)<br>
<br>
</div>You don't need the _sozcuk attribute at all here. It's just the same<br>
as the value of the unicode object itself. The code could be<br>
simplified to:<br>
<br>
class kelime(unicode):<br>
<div class="im"><br>
def __getattribute__(self, isim):<br>
att = super(kelime, self).__getattribute__(isim)<br>
if not callable(att):<br>
return att<br>
def sonra_cagir(*args, **kwargs):<br>
sonuc = att(*args, **kwargs)<br>
if isinstance(sonuc, basestring):<br>
return kelime(sonuc)<br>
return sonuc<br>
return sonra_cagir<br>
<br>
def cogul(self):<br>
</div> for harf in reversed(self):<br>
if harf in kalin:<br>
return kelime(self + u"lar")<br>
elif harf in ince:<br>
return kelime(self + u"ler")<br>
return kelime(self + u"lar")<br>
<br>
Also, "isinstance(sonuc, basestring)" should probably be<br>
"isinstance(sonuc, unicode)". Otherwise you'll break the encode<br>
method.<br>
<br>
If you want "kelime(u'one') + kelime(u'two')" to return a kelime<br>
instance, you'll need to override the __add__ special method as well.<br>
Likewise for "kelime(u'repeat') * 20" and the __mul__ method. You'll<br>
also want to override __getitem__ so that slicing returns a kelime<br>
instance as expected.<br>
<br>
Finally, I gather that the goal of this is not to modify the behavior<br>
of the unicode class at all, but just to add custom methods? I would<br>
strongly recommend that you not use a subclass for this, and instead<br>
just write some functions that take a string to operate on as an<br>
argument. Subclassing built-in types tends to be tricky as you can<br>
see, and this doesn't seem like a good reason to attempt it.<br>
<br>
Cheers,<br>
<font color="#888888">Ian<br>
</font></blockquote></div><br><br clear="all"><div><br></div>-- <br><a href="http://yasar.serveblog.net/" target="_blank">http://yasar.serveblog.net/</a><br><br>
</div>