Subclassing str object

Yaşar Arabacı yasar11732 at gmail.com
Wed Aug 31 13:49:20 EDT 2011


@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 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?

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.

@Terry: yes, my *args **kwargs method made tracebacks much less informative.
I agree on that. I am still trying to implement a better way.

@all:

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



31 Ağustos 2011 20:11 tarihinde Ian Kelly <ian.g.kelly at gmail.com> yazdı:

> 2011/8/31 Yaşar Arabacı <yasar11732 at gmail.com>:
> > I made a class like this (I shortened it just to show the point), what do
> > you think about it, do you think it is the python way of subclassing str
> (or
> > unicode in this case)
>
> You don't need the _sozcuk attribute at all here.  It's just the same
> as the value of the unicode object itself.  The code could be
> simplified to:
>
> class kelime(unicode):
>
>    def __getattribute__(self, isim):
>        att = super(kelime, self).__getattribute__(isim)
>        if not callable(att):
>            return att
>        def sonra_cagir(*args, **kwargs):
>            sonuc = att(*args, **kwargs)
>            if isinstance(sonuc, basestring):
>                return kelime(sonuc)
>            return sonuc
>        return sonra_cagir
>
>    def cogul(self):
>         for harf in reversed(self):
>            if harf in kalin:
>                return kelime(self + u"lar")
>            elif harf in ince:
>                return kelime(self + u"ler")
>        return kelime(self + u"lar")
>
> Also, "isinstance(sonuc, basestring)" should probably be
> "isinstance(sonuc, unicode)".  Otherwise you'll break the encode
> method.
>
> If you want "kelime(u'one') + kelime(u'two')" to return a kelime
> instance, you'll need to override the __add__ special method as well.
> Likewise for "kelime(u'repeat') * 20" and the __mul__ method.  You'll
> also want to override __getitem__ so that slicing returns a kelime
> instance as expected.
>
> Finally, I gather that the goal of this is not to modify the behavior
> of the unicode class at all, but just to add custom methods?  I would
> strongly recommend that you not use a subclass for this, and instead
> just write some functions that take a string to operate on as an
> argument.  Subclassing built-in types tends to be tricky as you can
> see, and this doesn't seem like a good reason to attempt it.
>
> Cheers,
> Ian
>



-- 
http://yasar.serveblog.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110831/5d78f3ae/attachment-0001.html>


More information about the Python-list mailing list