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

Mr. Joe titanix88 at gmail.com
Wed May 15 12:38:10 EDT 2013


On Wed, May 15, 2013 at 12:15 PM, dieter <dieter at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130515/baa42a07/attachment.html>


More information about the Python-list mailing list