[Python-ideas] The Descriptor Protocol...

Andrew Svetlov andrew.svetlov at gmail.com
Thu Mar 3 14:39:00 CET 2011


On Thu, Mar 3, 2011 at 2:23 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> But in
>>> class Person:
>>>  name as n = Str(n)
>> we still can see name duplication.
>
[...]
> However, compared to the status quo:
>
> 1. The backreference can use an arbitrarily short name, as it won't
> form part of the API*
> 2. This can be designed so the compiler helps out in detecting typos
> 3. If you later decide to change the public name (the real bane of the
> repeat-as-a-string workaround), it only needs to be changed in one
> place
>
That's true.

> *This can actually be done by using a pre-AST transform, such that the
> "n" variable never appears in the AST, instead being replaced by the
> constant string "name"
Not sure. Other usages of "as" put new variable in current scope:
import, with, except...

> I don't personally believe the use case is frequent enough to justify
> the complexity of a whole new statement type, but it's an interesting
> idea to consider regardless.

I use named descriptors (I mean objects aware about own name) time by
time in my data structures.
Last esage was about two weeks ago.
For example of that descriptors you can see SQLAlchemy
declarative_base, models and views from Django etc.
>From my perspective named descriptors are widelly used.
Of course current workaround with metaclass is good enough to don't
starve for better solution.

The same I can say about things like namedtuple. Current notation is
annoying me a bit, but I argee to live with that.



More information about the Python-ideas mailing list