Re: [Python-Dev] Summary of "dynamic attribute access" discussion

2007-02-13 Thread Greg Falcon
On 2/13/07, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
> Anthony Baxter schrieb:
> >> and the "wrapper class" idea of Nick Coghlan:
> >>attrview(obj)[foo]
> >
> > This also appeals - partly because it's not magic syntax 
>
> It's so easy people can include in their code for backwards
> compatibility; in Python 2.6, it could be a highly-efficient
> builtin (you still pay for the lookup of the name 'attrs',
> of course).

attrview() (or whatever name it ultimately gets) feels Pythonic to me.
 It's a lot cleaner than getattr/setattr/hasattr/delattr.  It's
perhaps not entirely as pretty as .[], but it's close enough that I
question whether adding new syntax is worth it.

The easy backwards compatibility is the huge feature here, but there
are a couple of others.

Because attrview() can be made to support much of the dict interface,
it can clearly express things the .[] syntax cannot, like:
* attrview(obj).get(key, default)
* attrview(obj).setdefault(key, []).append(x)

Also, because attrview objects implement a mapping, they can be used
in interesting ways in places that expect a mapping.  (The locals
parameter to eval() comes to mind, but I'm sure there would be many
other good uses we won't be able to predict here.)

Guido van Rossum wrote:
> Also, Nick's examples show (conceptual)
> aliasing problems: after "x = attrview(y)", both x and y refer to the
> same object, but use a different notation to access it.

Of course there is an aliasing here, but it's being explicitly
constructed, and so I fail to see what is problematic about it.  You
propose in PEP 3106 that Py3k's items/keys/values dictionary methods
should return wrapper objects, which provide a different interface to
the same data, and which can be used to mutate the underlying objects.
 Aren't the set of ailasing concerns in these two cases exactly the
same?

+1 for attrview(), +0 for .[] syntax. (-1 for .[] if attrview() is accepted.)

Greg F
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Wrapping up 'dynamic attribute' discussion

2007-02-15 Thread Greg Falcon
On 2/15/07, Anthony Baxter <[EMAIL PROTECTED]> wrote:
> On Friday 16 February 2007 09:08, Ben North wrote:
> > The wrapper class idea was left
> > open as a possibility for a future PEP.
>
> A good first step would be to contribute something like this to the
> Python Cookbook, if it isn't already there.

I could not find such a class in the cookbook.  (That's not to say
there's not one there that I missed.)

Because I think attrview() should happen, I submitted a recipe to the
Python Cookbook.  While it awaits editor approval, I have it posted at
http://www.verylowsodium.com/attrview.py .

One possibly controversial design choice here: since there is no
guaranteed way to enumerate attributes in the face of __getattr__ and
friends, my version of attrview() does not provide iteration or any
other operation that assumes object attributes can be enumerated.
Therefore, it technically does not implement a mapping.

Once Python grows a __dir__ special method, it's possible I could be
convinced this is the wrong choice, but I'm not sure of that.

Greg F
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com