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


Groups > comp.lang.python > #12502

Re: Subclassing str object

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To Python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'instance': 0.05; 'instance,': 0.05; 'attribute': 0.07; 'override': 0.07; 'subject:object': 0.07; 'python': 0.08; 'case)': 0.09; 'subclass': 0.09; 'tends': 0.09; 'def': 0.15; 'method.': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; 'encode': 0.16; 'expected.': 0.16; 'likewise': 0.16; 'simplified': 0.16; 'slicing': 0.16; 'subclassing': 0.16; 'cc:addr:python-list': 0.16; 'cheers,': 0.18; "doesn't": 0.22; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; '(or': 0.23; 'code': 0.25; 'string': 0.26; 'all,': 0.28; 'skip:_ 20': 0.28; 'modify': 0.28; 'gather': 0.29; 'unicode': 0.29; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'str': 0.30; 'class': 0.30; 'seem': 0.31; 'break': 0.32; 'it.': 0.33; 'probably': 0.33; 'instead': 0.33; 'see,': 0.34; 'object': 0.35; 'skip:" 10': 0.36; 'but': 0.37; 'could': 0.38; 'think': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'itself.': 0.39; "it's": 0.40; 'custom': 0.61; 'here.': 0.66; 'show': 0.67; 'special': 0.67; 'att': 0.84; 'methods?': 0.84; 'tricky': 0.84; 'charset:iso-8859-9': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=dZObqWKJAziJjzHpqu4Le/+Nnt51czBoCujLGB1iWdc=; b=Q0/cm99eajWhlZvE681aaxyuhizuekxIH9rOQ1xbzcqZ7ADDHdq6YxxjG9JH8cZc+I 254xDzN+JiX/5CkKUSR0MHlbWsNVYphofBedLx9lwUJZGSoohQqErOI2qhLBLd07hvjJ fdbr281Ua0h8H8Qj6x5hPCAcGE7tqwPs9gRS4=
MIME-Version 1.0
In-Reply-To <CAFEUn8YYTVeADOsZz+t24zsycNNyETYt-3m2ctZ_cYcPs-XT=w@mail.gmail.com>
References <CAFEUn8Z-9Qn=ZXS7e7C4AsNg=YOo78Jd5tca1_VkHRsfLyD1Aw@mail.gmail.com> <CAFEUn8YYTVeADOsZz+t24zsycNNyETYt-3m2ctZ_cYcPs-XT=w@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 31 Aug 2011 11:11:16 -0600
Subject Re: Subclassing str object
To Yaşar Arabacı <yasar11732@gmail.com>
Content-Type text/plain; charset=ISO-8859-9
Content-Transfer-Encoding quoted-printable
Cc python-list <Python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.618.1314810707.27778.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314810707 news.xs4all.nl 2500 [2001:888:2000:d::a6]:57443
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12502

Show key headers only | View raw


2011/8/31 Yaşar Arabacı <yasar11732@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web