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


Groups > comp.lang.python > #12501

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 <python-python-list@m.gmane.org>
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; 'method,': 0.07; 'received:verizon.net': 0.07; 'subject:object': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'say.': 0.09; 'str,': 0.09; 'subclass': 0.09; 'am,': 0.12; 'def': 0.15; 'class,': 0.15; "'a'": 0.16; '__init__': 0.16; 'enlighten': 0.16; 'methods,': 0.16; 'reedy': 0.16; 'str()': 0.16; 'tracebacks': 0.16; 'unmodified': 0.16; 'wrote:': 0.16; 'wrap': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'object,': 0.24; 'originally': 0.24; 'string': 0.26; 'posted': 0.26; 'bound': 0.29; '(and': 0.29; '(depending': 0.30; 'class': 0.30; 'objects': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; '...': 0.34; 'header :User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.35; 'question': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'put': 0.37; 'something': 0.37; 'two': 0.37; 'could': 0.38; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'your': 0.61; 'custom': 0.61; 'believe': 0.65; 'here': 0.65; 'here:': 0.65; 'chain': 0.66; 'standard,': 0.84; 'informative.': 0.96
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Subclassing str object
Date Wed, 31 Aug 2011 13:07:48 -0400
References <CAFEUn8Z-9Qn=ZXS7e7C4AsNg=YOo78Jd5tca1_VkHRsfLyD1Aw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0
In-Reply-To <CAFEUn8Z-9Qn=ZXS7e7C4AsNg=YOo78Jd5tca1_VkHRsfLyD1Aw@mail.gmail.com>
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.617.1314810531.27778.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314810531 news.xs4all.nl 2482 [2001:888:2000:d::a6]:48665
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12501

Show key headers only | View raw


On 8/31/2011 7:43 AM, Yaşar Arabacı wrote:
> Hİ,
>
> I originally posted my question to here:
> http://stackoverflow.com/q/7255655/886669 Could you people please look
> at it and enlighten me a little bit? I would appreciate an answer either
> from here or at stackoverflow.

I believe two people already gave my answer. If 'a' is bound to a str() 
object, "a.capitalize() will return a standard, unmodified str, not your 
custom class, so a.capitalize().mycustommethod() will fail.". If you 
reject that enlightenment, there is not much more to say. (And if you 
accept it, I am not sure what you still need.)

To put is a different way, if you want to chain together existing string 
methods and your new methods, you must start with objects of your new 
subclass and wrap every string method that you want to chain.

class mystr(str):
     ...
     def capitalize(s): return mystr(str.capitalize(s))

You ended up doing something like this in your edit #3, except you 
perhaps should not have the __init__ method (depending on what 'sozcuk' 
is) and you do the wrapping on every call instead of just once and use 
the generalized signature *arg,**kwds for every method, which will make 
tracebacks much less informative.

-- 
Terry Jan Reedy

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


Thread

Re: Subclassing str object Terry Reedy <tjreedy@udel.edu> - 2011-08-31 13:07 -0400

csiph-web