Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'true,': 0.05; '"""': 0.07; 'class,': 0.07; 'differently': 0.07; '(those': 0.09; '*args,': 0.09; 'back.': 0.09; 'code"': 0.09; 'decorator': 0.09; 'derived': 0.09; 'method,': 0.09; 'naturally': 0.09; 'override': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'cached': 0.16; 'descriptors.': 0.16; 'enough.': 0.16; 'means:': 0.16; 'name):': 0.16; 'overridden': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'subject:python': 0.16; 'bit': 0.19; "python's": 0.19; 'fit': 0.20; 'header:User-Agent:1': 0.23; 'decorators': 0.24; 'define': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'topic': 0.29; 'bunch': 0.31; 'writes:': 0.31; 'class': 0.32; 'quite': 0.32; 'cases': 0.33; 'comment': 0.34; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'too': 0.37; 'easily': 0.37; 'sometimes': 0.38; 'to:addr :python-list': 0.38; 'ability': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'experts': 0.60; 'new': 0.61; 'simply': 0.61; 'you.': 0.62; 'received:217': 0.63; 'name': 0.63; 'such': 0.63; 'natural': 0.68; 'behavior': 0.77; 'subject:skip:A 10': 0.78; 'decorate': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Date: Wed, 15 May 2013 08:15:53 +0200 References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e0b5a9.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:OLNwpmF1TCilQcGs6wDL/d3Sarc= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368598568 news.xs4all.nl 15902 [2001:888:2000:d::a6]:52748 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45324 "Mr. Joe" writes: > ... > Sorry for digging this old topic back. I see that my "'property' does not > play well with polymorphic code" comment generated some controversy. So > here's something in my defense: I did not intend to "attack" you. > ... > Yes, I like decorators and descriptors. I also like the ability to create a > "virtual property" in a python class by binding a bunch of methods as > setter/getter. But I find the implementation of this "virtual property > feature" a bit awkward sometimes - every time I need to override a > getter/setter in a child class, I need to decorate them again. Some of you > may like this "explicitness", but I don't. True, I have been hit by this, too - not with "property" but with other decorators (those for caching). But, after reflection, I came to the conclusion that I should be happy with this feature: If Python would automatically redecorate overridden methods in a derived class, I would have no control over the process. What if I need the undecorated method or a differently decorated method (an uncached or differently cached method, in my case)? Your getter/setter use case can quite easily be solved with a class "DelayedMethodAccessor": class DelayedMethodAccess(object): """ def __init__(self, name): self.__name = name def __call__(self, inst, *args, **kw): return getattr(inst, self.__name)(*args, **kw) You can then use: prop = property(DelayedMethodAccess(""), ...) Or define a new decorator "property_by_name" and then use prop = property_by_name("", ...) Of course, this requires that the property name and the getter/setter names differ, but this should be naturally enough. Python's current behavior is very natural once you know that decorators are just syntactic sugar and @ def ... simply means: def ... f = (f) True, this is no perfect fit for all use cases - but such a fit (if it existed at all) would have to be so complex that only experts could understand it.