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

yoav glazner yoavglazner at gmail.com
Sun Feb 5 16:33:48 CET 2012


>
> >> In a real module, you'd probably want to be more thorough about
> emulating a __dict__ dictionary though by adding item() and keys() etc.
> >
> > It's impossible in general.
> >
> > class A:
> >    def __getattr__(self, name):
> >        return len(name)
>
>
> >>> 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()
?!?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120205/3db0a8c0/attachment.html>


More information about the Python-ideas mailing list