[Python-ideas] Shorthand syntax for get/set/delattr (was Re: Dict-like object with property access)
simon.sapin at kozea.fr
Sun Feb 5 21:45:38 CET 2012
Le 05/02/2012 16:33, yoav glazner a écrit :
> >>> class proxy:
> ... def __init__(self, orig):
> ... self._orig = orig
> ... def __getitem__(self, attr):
> ... return getattr(self._orig,attr)
> >>> class A:
> ... def __getattr__(self, name):
> ... return len(name)
> >>> a = A()
> >>> proxy(a)['hello']
> Extending the proxy class to include setting, deleting, and various
> corner cases, is left as an exercise for the reader :-)
> >>> proxy(a).keys()
+1 on extending vars(). I like this idea much more than adding syntax.
In this case, proxy(a).key() would be based on dir(a) (or something
similar) and have the same (documented) limitations. I think this is
acceptable, and the proxy object is still useful.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas