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: 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 Subject: Re: Subclassing str object Date: Wed, 31 Aug 2011 13:07:48 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 8/31/2011 7:43 AM, Ya=C5=9Far Arabac=C4=B1 wrote: > H=C4=B0, > > 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 eithe= r > from here or at stackoverflow. I believe two people already gave my answer. If 'a' is bound to a str()=20 object, "a.capitalize() will return a standard, unmodified str, not your = custom class, so a.capitalize().mycustommethod() will fail.". If you=20 reject that enlightenment, there is not much more to say. (And if you=20 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=20 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=20 perhaps should not have the __init__ method (depending on what 'sozcuk'=20 is) and you do the wrapping on every call instead of just once and use=20 the generalized signature *arg,**kwds for every method, which will make=20 tracebacks much less informative. --=20 Terry Jan Reedy