[Python-Dev] PEP 463: Exception-catching expressions

Rob Cliffe rob.cliffe at btinternet.com
Mon Feb 24 18:52:43 CET 2014


On 22/02/2014 21:26, Tim Delaney wrote:
>
> On 23 February 2014 02:29, Nick Coghlan <ncoghlan at gmail.com 
> <mailto:ncoghlan at gmail.com>> wrote:
>
>     On 22 Feb 2014 22:15, "Stephen J. Turnbull" <stephen at xemacs.org
>     <mailto:stephen at xemacs.org>> wrote:
>     > Antoine Pitrou writes:
>     >  > Chris Angelico <rosuav at gmail.com <mailto:rosuav at gmail.com>>
>     wrote:
>     >  > > hasattr(x,"y") <-> (x.y or True except AttributeError: False)
>     >  > But it's not the same. hasattr() returns a boolean, not an
>     arbitrary
>     >  > value.
>     > I think he meant
>     >     hasattr(x,"y") <-> (x.y and True except AttributeError: False)
>
>     With PEP 463, the explicit equivalent of hasattr() would be
>     something like :
>
>     hasattr(x,"y") <-> (bool(x.y) or True except AttributeError: False)
>
> That would work, but I think I'd prefer:
>
> hasattr(x,"y") <-> bool(x.y or True except AttributeError: False)
>
> Makes it clearer IMO that the entire expression will always return a 
> boolean.
>
This also works:

hasattr(x,"y")  <->  (lambda v: True)(x.y) except AttributeError: False

Which is less obscure is a matter of judgement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140224/3367a447/attachment.html>


More information about the Python-Dev mailing list