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


Groups > comp.lang.python > #12529

Re: Subclassing str object

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(using': 0.05; '*not*': 0.05; 'encoded': 0.05; 'instance': 0.05; 'instance,': 0.05; 'ascii': 0.07; 'attribute': 0.07; 'method,': 0.07; 'override': 0.07; 'subject:object': 0.07; 'python': 0.08; 'encodes': 0.09; 'received:mail-bw0-f46.google.com': 0.09; 'silently': 0.09; 'class,': 0.15; 'expected.': 0.16; 'lucky,': 0.16; 'object).': 0.16; 'overwriting': 0.16; 'received:209.85.214.46': 0.16; 'cc:addr:python-list': 0.16; 'wrap': 0.18; "doesn't": 0.22; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'thus': 0.23; 'byte': 0.24; 'object,': 0.24; 'creating': 0.25; 'string': 0.26; 'work.': 0.27; 'all,': 0.28; 'received:209.85.214': 0.28; 'bit': 0.28; 'unicode': 0.29; 'looks': 0.29; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'example': 0.30; 'broke': 0.30; 'comments.': 0.30; 'grasp': 0.30; 'object.': 0.30; 'str': 0.30; 'thanks': 0.30; 'class': 0.30; "didn't": 0.31; 'does': 0.32; "isn't": 0.33; 'it.': 0.33; 'there': 0.33; 'instead': 0.33; 'creates': 0.34; 'latter': 0.34; 'uses': 0.35; 'object': 0.35; 'explain': 0.36; 'using': 0.37; 'something': 0.37; 'could': 0.38; 'think': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'might': 0.40; "it's": 0.40; 'back': 0.62; 'special': 0.67; 'encoding,': 0.84; 'wonder,': 0.84
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=ofhEwWVbXsa3usSkOhaaxnExLmyMasGJKlB0VFfajZc=; b=EeWxCCn4gPguVddKOsGTWwizrWCQx5IUiL2Mk1vrhxffa6AmVhK0lxXjpmFkjUIYpp kc/WA0WorFcpv7x6pgvFpwr/gR/Gc4bJ9bMvR60ra5/OIE3qlIlWBYKQ+UTUZg2PE/9N cNC3XxL4wHC0vbo1CMII+7iUR07CLvlwUXgyE=
MIME-Version 1.0
In-Reply-To <CAFEUn8YVU=CctHO+H3v25CfBoYSJJZem7UBbYUmM=BsgEd0uzg@mail.gmail.com>
References <CAFEUn8Z-9Qn=ZXS7e7C4AsNg=YOo78Jd5tca1_VkHRsfLyD1Aw@mail.gmail.com> <CAFEUn8YYTVeADOsZz+t24zsycNNyETYt-3m2ctZ_cYcPs-XT=w@mail.gmail.com> <CALwzidmeVu9c5HsfVMqCT3Az_fbdervj0_YwxPwfmoceX074rA@mail.gmail.com> <CAFEUn8YVU=CctHO+H3v25CfBoYSJJZem7UBbYUmM=BsgEd0uzg@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 31 Aug 2011 16:16:54 -0600
Subject Re: Subclassing str object
To Yaşar Arabacı <yasar11732@gmail.com>
Content-Type text/plain; charset=UTF-8
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.633.1314829046.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314829046 news.xs4all.nl 2431 [2001:888:2000:d::a6]:54641
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12529

Show key headers only | View raw


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.

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 16:16 -0600

csiph-web