[Python-ideas] Shorthand syntax for get/set/delattr (was Re: Dict-like object with property access)

Simon Sapin simon.sapin at kozea.fr
Sun Feb 5 22:30:38 CET 2012

Le 05/02/2012 22:18, Paul Moore a écrit :
> In my view, following the spec of vars() is far more useful and
> matches better the original requirement, which was to simulate
> javascript's index/attribute duality. Methods (and even more so
> special methods) don't really fit in here.
> I'd argue for the definition:
> proxy(obj)['a']<=>  obj.a
> proxy(obj)['a' = val]<=>  obj.a = val
> del proxy(obj)['a']<=>  del obj.a
> 'a' in proxy(obj)<=>  hasattr(obj, 'a')
> proxy(obj).keys()<=>  vars(obj).keys()
> len(proxy(obj))<=>  len(vars(obj))

I’m fine with that too and I agree it is probably better. My point was 
that not all keys that can be used in proxy(a)[key] without KeyError 
will be in proxy(a).keys(), but that’s okay because the same already 
happens with getattr() and dir()

By the way, the proxy should also turn AttributeError into KeyError, for 
consistency with other Mapping types.


Simon Sapin

More information about the Python-ideas mailing list