[Python-Dev] Re: __except__ use cases

John J Lee jjl at pobox.com
Sun Apr 24 19:12:03 CEST 2005


On Sun, 24 Apr 2005, Nick Coghlan wrote:
[...]
> Seeing this example has convinced me of something. PEP 310 should use the 'with' 
> keyword, and 'expression block' syntax should be used to denote the 'default 
> object' semantics proposed for Python 3K. For example:
> 
> class Key2AttributeError(object):
>      def __init__(self, obj, attr):
>          self:
>              .obj_type = type(obj)
>              .attr = attr
>      def __except__(self, ex_type, ex_val, ex_tb):
>          if isinstance(ex_type, KeyError):
>               self:
>                   raise AttributeError("%s instance has no attribute %s"
>                                         % (.obj_type, .attr))
> 
> > # Somewhere else. . .
>      def __getattr__(self, name):
>          with Key2AttributeError(self, key):
>              self:
>                  return ._cache[key]
[...]

+1

Purely based on my aesthetic reaction, that is.  Never having used other
languages with this 'attribute lookup shorthand' feature, that seems to
align *much* more with what I expect than the other way around.  If 'with'
is used in other languages as the keyword for attribute lookup shorthand,
though, perhaps it will confuse other people, or at least make them frown
:-(


John


More information about the Python-Dev mailing list