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


Groups > comp.lang.python > #45355

Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError.

References (1 earlier) <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <mailman.1265.1367567552.3114.python-list@python.org> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> <CAJxoosf=ZMK2cVuGdYWH7Hv5tS9pN+ZKzhFvKCerbQuNrK_y5w@mail.gmail.com> <87bo8cvkdi.fsf@handshake.de>
Date 2013-05-15 22:38 +0600
Subject Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError.
From "Mr. Joe" <titanix88@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1706.1368635899.3114.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wed, May 15, 2013 at 12:15 PM, dieter <dieter@handshake.de> 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("<getter_name>"), ...)
>
> Or define a new decorator "property_by_name" and then
> use
>
>    prop = property_by_name("<getter_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
>
>    @<decorator_expression>
>    def <f>...
>
> simply means:
>
>    def <f>...
>    f = <decorator_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.

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


Thread

Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-03 05:34 +0600
  Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-03 00:20 +0000
    Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-03 13:52 +0600
      Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-04 06:29 +0000
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-14 21:09 +0600
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. dieter <dieter@handshake.de> - 2013-05-15 08:15 +0200
        Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. "Mr. Joe" <titanix88@gmail.com> - 2013-05-15 22:38 +0600
    Re: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. dieter <dieter@handshake.de> - 2013-05-04 07:56 +0200

csiph-web