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

dieter dieter at handshake.de
Sat May 4 01:56:22 EDT 2013


"Mr. Joe" <titanix88 at gmail.com> writes:

> ...
> Then I came to know that 'property' does not play well
> with polymorphic code. :(

Can you elaborate?

I like "polymorphic code" and decorators (such a "property")
never met a problem with the two working nicely together.

> I resorted to some lambda hacks learned from
> stackoverflow.com to solve the problem. I know that it's the correct way
> for decorators to work, but still, it would be nice to have a language
> level solution.

There are two approaches: change the language or learn how the language works.

I am very happy that the Python developers try hard to retain backward
compatible and, consequently, are very reluctant towards language changes.
I would hate should the decorator behavior change in an incompatible
way.

On the other hand, decorators are very easy to understand: they are
just syntactic sugar:

     @<dec_expression>
     def f(...): ...

  is equivalent to:

     def f(...): ...
     f = <dec_expression>(f)

The "property" decorator uses an additional concept: "descriptor"s.
A "descriptor" allows you to customize attribute access.

These two concepts graped, the "property" decorator should no longer
cause surprises -- even in "polymorphic code".




More information about the Python-list mailing list