[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 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']
>     5
>     >>>
>
>     Extending the proxy class to include setting, deleting, and various
>     corner cases, is left as an exercise for the reader :-)
>
>
> >>> proxy(a).keys()
> ?!?

Hi,

+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.

Regards,

-- 
Simon Sapin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120205/3bb3ff98/attachment.html>


More information about the Python-ideas mailing list