Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.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; 'true,': 0.05; '"""': 0.07; 'class,': 0.07; 'differently': 0.07; 'patterns.': 0.07; 'skip:p 60': 0.07; '*args,': 0.09; 'decorator': 0.09; 'derived': 0.09; 'method,': 0.09; 'naturally': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'argument.': 0.16; 'cached': 0.16; 'enough.': 0.16; 'fits': 0.16; 'means:': 0.16; 'name):': 0.16; 'overridden': 0.16; 'subject: \n ': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'wed,': 0.18; "python's": 0.19; 'skip:p 40': 0.19; 'fit': 0.20; 'cc:addr:python.org': 0.22; 'decorators': 0.24; 'cc:2**0': 0.24; '>': 0.26; '15,': 0.26; 'define': 0.26; 'second': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'quite': 0.32; 'cases': 0.33; 'skip:& 30': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'received:209': 0.37; 'easily': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'skip:p 20': 0.39; '8bit%:6': 0.40; 'experts': 0.60; 'new': 0.61; 'simply': 0.61; 'name': 0.63; 'such': 0.63; 'natural': 0.68; 'behavior': 0.77; 'subject:skip:A 10': 0.78; 'to:none': 0.92; 'convinced': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:cc:content-type; bh=T8aLMfpDnJ+rSiEuum+xAd+PS0cFEkzPSjut/6qtJFM=; b=apMVUMSmLAVZZ/pg6p0fRj8vsMJlVncEu2aG8cIcVFyrLEvDIQbfupOY4zJgrDlf9e 7HCsGZDfYdAgF4TWlmamAX3uwVBv8rxZk1Keo8n3MH1OopOrqMSfHO6nF7LHCh8A4r1d 5NoFShVTs7ENRIc+/L1GZxIlWWKrYRTaJty1Ly4Wh2RHBqeTcJcFlDVfgqJmcOpAu17r cD3cR5PA7aNxPzd1/9ZVjYW08Xn/utCSBiyhdkGOZkyFKxqocPpQcQ30dDkQWWZUgIL3 rggpg3dMXkiTTZxB3/NzPfYI0gSk9BWZwdZqSO7YxanTvmnmoBD+1llnoaYWH6nfF3f/ rxlA== MIME-Version: 1.0 X-Received: by 10.68.193.138 with SMTP id ho10mr39322276pbc.163.1368635890680; Wed, 15 May 2013 09:38:10 -0700 (PDT) In-Reply-To: <87bo8cvkdi.fsf@handshake.de> References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> <87bo8cvkdi.fsf@handshake.de> Date: Wed, 15 May 2013 22:38:10 +0600 Subject: Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. From: "Mr. Joe" Cc: python-list Content-Type: multipart/alternative; boundary=047d7b15af4317480b04dcc46023 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368635899 news.xs4all.nl 15988 [2001:888:2000:d::a6]:37015 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45355 --047d7b15af4317480b04dcc46023 Content-Type: text/plain; charset=UTF-8 On Wed, May 15, 2013 at 12:15 PM, dieter wrote: > > 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)? > On a second thought, I am convinced by your argument. > 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. Thanks for these really nice patterns. They fits my problem very well. --047d7b15af4317480b04dcc46023 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

On Wed, May 15,= 2013 at 12:15 PM, dieter <dieter= @handshake.de> wrote:
>
> =C2=A0 If Python would automat= ically redecorate overridden methods in a derived
> =C2=A0 class, I would have no control over the process. What if I need=
> =C2=A0 the undecorated method or a differently decorated method (a= n
> =C2=A0 uncached or differently cached method, in my case)?
>= ;

On a second thought, I am convinced by your argument.

> Your getter/setter use case can quite easily be solved
> wit= h a class "DelayedMethodAccessor":
>
> =C2=A0 class D= elayedMethodAccess(object):
> =C2=A0 =C2=A0 """
>= ; =C2=A0 =C2=A0 def __init__(self, name): self.__name =3D name
> =C2=A0 =C2=A0 def __call__(self, inst, *args, **kw):
> =C2=A0 = =C2=A0 =C2=A0 return getattr(inst, self.__name)(*args, **kw)
>
>= ; You can then use:
>
> =C2=A0 =C2=A0prop =3D property(DelayedM= ethodAccess("<getter_name>"), ...)
>
> Or define a new decorator "property_by_name" and the= n
> use
>
> =C2=A0 =C2=A0prop =3D property_by_name("= <getter_name>", ...)
>
> Of course, this requires th= at the property name and the getter/setter names
> differ, but this should be naturally enough.
>
>
> P= ython's current behavior is very natural once you know that
> dec= orators are just syntactic sugar and
>
> =C2=A0 =C2=A0@<deco= rator_expression>
> =C2=A0 =C2=A0def <f>...
>
> simply means:
>> =C2=A0 =C2=A0def <f>...
> =C2=A0 =C2=A0f =3D <decorato= r_expressen>(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.

Thanks for these really = nice patterns. They fits my problem very well.

--047d7b15af4317480b04dcc46023--